Swift: 初始化器中的自身具有类型 (T) -> () -> T
Swift: self in initializer has type (T) -> () -> T
TL;DR:
NSObject
的 self
变量如何获取类型 (T) -> () -> T
?
备注
我明白了为什么使用self
这种方式是不合法的。但我正在尝试理解第二条错误消息。
代码
struct DummyStorer {
let dummy: Dummy
}
struct Dummy {
let storer = DummyStorer(dummy: self)
// Use of unresolved identifier 'self'
// OK, that's reasonable. But...
}
class Dummy : NSObject {
let storer = DummyStorer(dummy: self)
// Cannot convert value of type '(Dummy) -> () -> Dummy' to expected argument type 'Dummy'
// ... how does the compiler arrive at this?
}
它是 NSObjectProtocol
的一部分
public protocol NSObjectProtocol {
func isEqual(_ object: Any?) -> Bool
var hash: Int { get }
var superclass: AnyClass? { get }
func `self`() -> Self // << here !!
TL;DR:
NSObject
的 self
变量如何获取类型 (T) -> () -> T
?
备注
我明白了为什么使用self
这种方式是不合法的。但我正在尝试理解第二条错误消息。
代码
struct DummyStorer {
let dummy: Dummy
}
struct Dummy {
let storer = DummyStorer(dummy: self)
// Use of unresolved identifier 'self'
// OK, that's reasonable. But...
}
class Dummy : NSObject {
let storer = DummyStorer(dummy: self)
// Cannot convert value of type '(Dummy) -> () -> Dummy' to expected argument type 'Dummy'
// ... how does the compiler arrive at this?
}
它是 NSObjectProtocol
public protocol NSObjectProtocol {
func isEqual(_ object: Any?) -> Bool
var hash: Int { get }
var superclass: AnyClass? { get }
func `self`() -> Self // << here !!