Fast initialization of objects (factory class, init by default, init convenience)

Hi I'm trying to find the best example for working with objects in Swift.

I think I understood the initializers correctly, both convenience and default ... but what happens to the methods of the factory class?

I tried to create a simple Person class and a Student subclass with multiple properties and methods. is this the most correct way to do it?

   class Person{

    var _name: String
    var _surname: String
    var _dateOfBirthday: String
    var _phoneNumb: [String]

    init(name:String, surname:String, dateOfBirthday:String, phone:[String]){
        self._name = name
        self._surname = surname
        self._dateOfBirthday = dateOfBirthday
        self._phoneNumb = phone
    }

    convenience init() {
        self.init(name:"",surname:"",dateOfBirthday:"", phone:[])
    }

    convenience init(name:String){
        self.init(name:name,surname:"",dateOfBirthday:"", phone:[])
    }


}



class Student:Person{

    var _studentId:Int

    init(name: String, surname: String, dateOfBirthday: String, phone: [String], id:Int) {
        self._studentId = id
        super.init(name: "", surname: "", dateOfBirthday: "", phone: [])
    }

    convenience init(){
        self.init(name: "", surname: "", dateOfBirthday: "", phone: [], id:0)
    }

    convenience init(name:String){
        self.init(name:name,surname:"",dateOfBirthday:"", phone:[], id:0)
    }

}

      

what if i want to add a factory class method? would it be something like this, or am I doing it wrong?

class func Person() -> Person {
 var x = Person()
 x._telephoneNumber = [String]() // is this needed? or i can initialize it later?
 return x
}

class func PersonWithName(name:String) -> Person {
 var x = Person(name:name, surname:"", dateOfBirthday:"", telephoneNumber:[])
 return x
}

      

It is right? why would it be better to use init instead of a factory class?

+3


source to share


2 answers


It is right? why would it be better to use init instead of a factory class?

Why would you create a "class factory" when you can use init? init

is an idiomatic fast way to create new class objects.

Adding convenience initializers is the right choice in most cases when you want to add a shortcut to the main (designated) class initializer. However, in your case they are completely unnecessary since Swift supports default argument values.

Just define your initializer like this:

init(name:String = "", surname:String = "", dateOfBirthday:String = "", phone:[String] = []) { ... }

      



Thus, you can call it like Person()

or Person(name: "Andrew")

or with any other combination of arguments.

EDIT:

As a side note, underscore prefixed instance variables usually don't seem like Swift idiomatic. It's good to omit the underscore and use it self.

to disambiguate between local and instance variables:

self.name = name
self.surname = surname
self.dateOfBirthday = dateOfBirthday
self.phoneNumb = phone

      

+2


source


Until recently XCode 6.1 and Swift 1.1, it was necessary to use a factory pattern if the build could fail because it init()

cannot return options. This is why many imported cocoa / objective-c libraries had factory methods.

With the release of XCode 6.1 and Swift 1.1 and support for init()

options that can return, you should use a template init()

with convenience initializers. In this release, Apple also changed the cocoa / objective-c import to use a init() -> T?

factory -defined pattern .



See https://developer.apple.com/library/ios/releasenotes/DeveloperTools/RN-Xcode/Chapters/xc6_release_notes.html for release notes.

0


source







All Articles