7

I want to know the difference between the NSObject and struct..Following example will explain both cases

In struct

struct UserDetails{
    var userName:String
    var userID:String
    var userAge:String
    func userDescription()->String{
        return "name " + userName + "age " + userAge
    }
}

In NSObject Class

class UserDetails: NSObject {
    var userName:String?
    var userID:String?
    var userAge:String?
    func userDescription()->String{
        return "name " + userName! + "age " + userAge!
    }
}

Can you anyone please tell me where I have to use NSObject class, where I have to use struct..?

Grimxn
  • 22,115
  • 10
  • 72
  • 85
Mani murugan
  • 1,792
  • 2
  • 17
  • 32

2 Answers2

6

1) Structs are passed by value, Class instances by reference 2) Classes can be subclassed, Structs can't.

Whether or not the Class is a subclass of NSObject is (mostly) irrelevant. You could equally have said:

class UserDetails {
    var userName:String?
    var userID:String?
    var userAge:String?
    func userDescription()->String{
        return "name " + userName! + "age " + userAge!
    }
}
Grimxn
  • 22,115
  • 10
  • 72
  • 85
  • 1
    I'm not sure that there's much in it - it's really a design decision. If you are ever going to need to subclass, Class is the way to go. If you know you will never subclass, you can use a Struct. – Grimxn Jun 24 '14 at 07:58
  • 1
    Neither. Both have good and bad aspects for performance, depending on the situation (classes require reference counting, pointer indirection, and memory allocation, but structs require making copies). – Catfish_Man Jun 24 '14 at 07:58
  • 1
    @Grimxn in other words, if you pass a lot of structs around all over the place they will be much MUCH slower than objects and use significantly more RAM. If you do not pass them around much, then structs will be faster and use less RAM. Basically, if your code is too slow or uses too much memory you should consider testing the other. – Abhi Beckert Jun 24 '14 at 08:09
  • @newacct I'm confused by your usage "you can only manipulate them through pointers", since (despite the underlying representation) one of the major themes of Swift is to remove the concept of pointers from developers' semantics when using the language. – Grimxn Jun 27 '14 at 22:10
  • @Grimxn: They are pointers, no matter what you call them. Pointer = something that points to something. "Reference type" means a type whose values are references (pointers). – newacct Jun 27 '14 at 23:45
5

Classes and structs in Swift are much closer than in many languages. Both can have properties, method, initializers, subscripts, can conform to protocols, and can be extended. However, only classes can take advantage of inheritance and use deinitializers, and because classes are used by reference, you can have more than one reference to a particular instance.

Structs are used throughout Swift -- arrays, dictionaries, everything optional, and more are built on the struct type, so performance should be very high. You can use struct whenever you don't need the inheritance or multiple references that classes provide.

Nate Cook
  • 92,417
  • 32
  • 217
  • 178