xcode Swift 对象初始化(类工厂方法,默认初始化,便捷初始化)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26634205/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Swift Objects initialization (Class factory method, default init, convenience init)
提问by Swifterino
Hi i'm trying to figure out the best pattern to work with objects in Swift.
嗨,我正在尝试找出在 Swift 中处理对象的最佳模式。
i think i got it right with the initializers, both convenience and default... but what happen with the class factory methods?
我想我用初始化器做对了,方便和默认......但是类工厂方法会发生什么?
I tried to create a simple class Person and a subclass Student, with few properties and methods. is it the most correct way to do it?
我试图创建一个简单的类 Person 和一个子类 Student,几乎没有属性和方法。这是最正确的方法吗?
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 class factory method? would it be something like this or i'm 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
}
is this correct? why would it be better to use the init instead of the class factory?
这样对吗?为什么使用 init 而不是类工厂会更好?
采纳答案by radex
is this correct? why would it be better to use the init instead of the class factory?
这样对吗?为什么使用 init 而不是类工厂会更好?
Why would you create a "class factory" if you can use init? init
is idiomatic Swift way of creating new objects of a class.
如果可以使用 init,为什么要创建“类工厂”?init
是创建类的新对象的惯用 Swift 方式。
Adding convenience initializers is the right choice in most cases when you want to add a shortcut to class's main (designated) initializer. However, in your case, they are completely unnecessary, because Swift supports default argument values.
在大多数情况下,当您想向类的主要(指定)初始化程序添加快捷方式时,添加便利初始化程序是正确的选择。但是,在您的情况下,它们完全没有必要,因为 Swift 支持默认参数值。
Just define your initializer like so:
只需像这样定义您的初始化程序:
init(name:String = "", surname:String = "", dateOfBirthday:String = "", phone:[String] = []) { ... }
This way, you can invoke it as Person()
or Person(name: "Andrew")
or with any other combination of arguments.
这样,您可以将其作为Person()
或Person(name: "Andrew")
或与任何其他参数组合一起调用。
EDIT:
编辑:
As a side note, prefixing instance variables with an underscore generally doesn't seem to be idiomatic Swift. It's okay to omit the underscore and use self.
to disambiguate between local and instance variables:
作为旁注,使用下划线作为前缀实例变量通常似乎不是惯用的 Swift。可以省略下划线并用于self.
消除局部变量和实例变量之间的歧义:
self.name = name
self.surname = surname
self.dateOfBirthday = dateOfBirthday
self.phoneNumb = phone
回答by tng
Prior to the recent XCode 6.1 and Swift 1.1, it was necessary to use factory pattern if construction could fail because init()
could not return optionals. This was also why many cocoa/objective-c libraries imported had factory methods.
在最近的 XCode 6.1 和 Swift 1.1 之前,如果由于init()
无法返回可选项而导致构造失败,则必须使用工厂模式。这也是为什么许多导入的 cocoa/objective-c 库都有工厂方法的原因。
With the release of XCode 6.1 and Swift 1.1 and the support for init()
that can return optionals, you should use the init()
pattern with convenience initializers. With this release, Apple also changed their cocoa/objective-c imports to use the init() -> T?
pattern over the factory method.
随着 XCode 6.1 和 Swift 1.1 的发布以及对init()
可以返回选项的支持,您应该使用init()
带有便利初始化程序的模式。在此版本中,Apple 还更改了他们的 cocoa/objective-c 导入,以init() -> T?
在工厂方法上使用该模式。
See https://developer.apple.com/library/ios/releasenotes/DeveloperTools/RN-Xcode/Chapters/xc6_release_notes.htmlfor the release notes.