Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Subclassing NSObject in Swift - Best Practice with Initializers

Here is the layout of an example Class, can someone guide me on what's best practice when creating a subclass of NSObject?

class MyClass: NSObject {

    var someProperty: NSString! = nil

    override init() {
        self.someProperty = "John"
        super.init()
    }

    init(fromString string: NSString) {
        self.someProperty = string
        super.init()
    }

}

Is this correct, am I following best practice here?

I wonder if I'm correctly setting up the initializers (one that sets the string to a default, and one which I can pass in a string)?

Should I call super.init() at the end of each of the initializers?

Should my more specific (the one that takes a string) initializer simply call self.init() at the end rather than super.init()?

What is the right way to set up the initializers in Swift when subclassing NSObject? - and how should I call the super init ?

This question (albeit in Objective C) suggests you should have an init, which you always call and simply set the properties in more specific inits: Objective-C Multiple Initialisers

like image 610
Woodstock Avatar asked Aug 16 '14 19:08

Woodstock


People also ask

Do Swift classes inherit from NSObject?

You don't have to inherit from NSObject in Swift, but you did in Objective-C and in fact there are some behaviors you can only have if you do inherit from it. More on that in project 12, but for now just make sure you inherit from NSObject .

What is required Initializers in Swift?

An initializer is a special type of function that is used to create an object of a class or struct. In Swift, we use the init() method to create an initializer. For example, class Wall { ... // create an initializer init() { // perform initialization ... } }

Why do we need NSObject in Swift?

Subclassing NSObject in Swift gets you Objective-C runtime flexibility but also Objective-C performance. Avoiding NSObject can improve performance if you don't need Objective-C's flexibility.

What is NSObject in Swift?

The root class of most Objective-C class hierarchies, from which subclasses inherit a basic interface to the runtime system and the ability to behave as Objective-C objects.


2 Answers

I'm not Swift ninja but I would write MyClass as:

class MyClass: NSObject {          var someProperty: NSString // no need (!). It will be initialised from controller           init(fromString string: NSString) {         self.someProperty = string         super.init() // can actually be omitted in this example because will happen automatically.     }          convenience override init() {         self.init(fromString:"John") // calls above mentioned controller with default name     }         } 

See the initialization section of the documentation

like image 80
Maxim Shoustin Avatar answered Sep 29 '22 22:09

Maxim Shoustin


If someProperty can be nil, then I think you want to define the property as:

var someProperty: NSString?

This also eliminates the need for a custom initializer (at least, for this property), since the property doesn't require a value at initialization time.

like image 41
Jed Lau Avatar answered Sep 29 '22 23:09

Jed Lau