试过这个,它给了我一个错误:
class BaseClass {
class var testProperty: String {
return "Original"
}
class func testingFunc()->Self {
return self // error - Can not convert return expression of type "Self.Type" to return the type "Self"
}
}
有什么想法吗? 谢谢
最佳答案
在类/静态函数中,self
指类类型。没有实例可以引用,所以得到的是类型,也就是当前作用域。它在实例方法中不一样,其中 self 指的是 <instance>.self
class Foo {
class func classMethod() -> Foo.Type {
return self // means Foo.self
}
func instanceMethod() -> Foo {
return self // means <instance of Foo>.self
}
}
关于ios - 如何在类方法中返回 self ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34845113/