你可能知道Swift中的初始化规则是非常严格的(如果你问我就太严格了)。我发现自己处于不得不初始化一个需要关闭自己的let属性的情况,当然这个抱怨者抱怨。类似下面的例子。
let aString: String
let delete: Action<Int, Int, NoError>
init(aString: String) {
self.aString = aString
self.delete = Action { [unowned self](index) in
return self.performDeletion(index)
}
}
func performDeletion(index: Int) -> SignalProduer<Int, NoError> {
// Some deletion logic
}
唯一的解决方案&#34;我发现是使用像这样的私有变量
let aString: String
private let _delete: Action<Int, Int, NoError>
var delete: Action<Int, Int, NoError> {
return _delete!
}
init(aString: String) {
self.aString = aString
_delete = Action { [unowned self](index) in
return self.performDeletion(index)
}
}
func performDeletion(index: Int) -> SignalProduer<Int, NoError> {
// Some deletion logic
}
还有其他解决方案吗?