objective-c 如何在 Swift 中返回实例类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24180768/
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
How can I return instancetype in Swift
提问by Zoon Nooz
I want to make an extension of some class to return the runtime type object
我想扩展某个类以返回运行时类型对象
for example, I create extenstion of class A
例如,我创建了 A 类的扩展
extension A {
class func niceObject() -> XXXXX { // in objective-c is instancetype
return ....
}
}
So anyone know there is instancetype keyword in Swift or not to replace xxxxxand I can call this function on subclass of A without manually cast
所以任何人都知道 Swift 中有 instancetype 关键字或不替换xxxxx,我可以在 A 的子类上调用这个函数而无需手动转换
var b: B = B.niceObject()
Thanks
谢谢
采纳答案by Grimxn
You can do it. Playground code below. It's self() that niceObject() has to return. Additionally, you must have a requiredinit on the base class.
你能行的。游乐场代码如下。niceObject() 必须返回 self() 。此外,您必须required在基类上有一个init。
class A {
required init() {
}
func whatClassAmI() -> String {
return "Class A"
}
}
class B: A {
required init() {
super.init()
}
override func whatClassAmI() -> String {
return "Class B"
}
}
let a = A()
let sa = a.whatClassAmI() // "Class A", of course
let b = B()
let sb = b.whatClassAmI() // "Class B", of course
extension A {
class func niceObject() -> Self {
return self.init()
}
}
let aa = A.niceObject()
let saa = aa.whatClassAmI() // "Class A"
let bb = B.niceObject()
let sbb = bb.whatClassAmI() // "Class B", as required
回答by Sulthan
There is a keyword Selfwhich is allowed in two places - in protocols (see Jean-Philippe Pellet's answer) and as a result of classmethods:
有Self两个地方允许使用关键字- 在协议中(参见 Jean-Philippe Pellet 的回答)和作为class方法的结果:
extension A {
class func niceObject() -> Self? {
return nil
}
}
Unfortunately, this won't help you because the following is invalid
不幸的是,这对您没有帮助,因为以下内容无效
extension A {
class func niceObject() -> Self? {
//A can't be converted to Self
return A()
}
}
The error is caused by the fact that when you inherit from A
该错误是由以下事实引起的:当您继承自 A
class B : A {
}
then
然后
var b = B.niceObject()
would actually return an Ainstance which is not convertible to Self(Selfis B)
实际上会返回一个A不可转换为Self( Selfis B)的实例
@Grimxn found the correct solution (See his answer):
@Grimxn 找到了正确的解决方案(见他的回答):
You have to add a required initializer to the base class, e.g.
您必须向基类添加所需的初始化程序,例如
class A {
@required init() {
}
}
and then you can call the initialiser using self()
然后你可以使用调用初始化程序 self()
extension A {
class func niceObject() -> Self {
return self()
}
}
回答by Jean-Philippe Pellet
At least in procotols, you can use Self. This represents the actual type of self. Not sure about extensions, though...
至少在协议中,您可以使用Self. 这代表 的实际类型self。不确定扩展,但...
For instance, see the definition of Equatable:
例如,请参见 的定义Equatable:
protocol Equatable {
func ==(lhs: Self, rhs: Self) -> Bool
}
回答by fulvio
When you use a Swift API from Objective-C, the compiler typically performs a direct translation. For example, the Swift API func playSong(name: String)is imported as - (void)playSong:(NSString *)namein Objective-C.
当您使用 Objective-C 的 Swift API 时,编译器通常会执行直接转换。例如,Swift APIfunc playSong(name: String)就像- (void)playSong:(NSString *)name在 Objective-C 中一样导入。
However, there is one exception: When you use a Swift initializerin Objective-C, the compiler adds the text “initWith” to the beginning of the method and properly capitalizes the first character in the original initializer.
但是,有一个例外:当您在 Objective-C 中使用Swift 初始化程序时,编译器将文本“ initWith”添加到方法的开头,并正确地将原始初始化程序中的第一个字符大写。
For example, this Swift initializer init (songName: String, artist: String)is imported as - (instancetype)initWithSongName:(NSString *)songName artist:(NSString *)artistin Objective-C.
例如,这个 Swift 初始值设定项init (songName: String, artist: String)是- (instancetype)initWithSongName:(NSString *)songName artist:(NSString *)artist在 Objective-C 中导入的。

