我有一個struct
建立一個接受參考作爲單個初始化參數:參考`self`在夫特實例成員聲明
internal struct NodeState: Equatable {
weak var node: Node! = nil
// ...
init(node: Node) {
self.node = node
}
}
欲實例化一個NodeState
作爲Node
類的成員,並傳遞self
中設置弱引用:
public class Node: NSObject {
internal var state = NodeState(node: self)
// ...
}
...但我得到這個奇怪的編譯錯誤:
Cannot convert value of type 'NSObject ->() -> Node' to expected argument type 'Node'
我不允許在Swift的成員聲明中引用self
?
[初始化需要「自我」作爲參數斯威夫特特性]的可能的複製(http://stackoverflow.com/questions/25149248/initializing-swift-properties-that-require-self作爲一個參數) – jtbandes
只是要注意,奇怪的編譯錯誤是由於[NSObject中的'self'方法](https://developer.apple.com/reference/objectivec/nsobjectprotocol/1418954-self)。在非ObjC類中,錯誤將僅僅是「使用解析標識符self'」。 – kennytm