有人可以告訴我爲什麼Swift必須調用一個屬性的setter,當它僅用於訪問一個對象(協議)以設置其屬性之一時?第一個例子說明,如果我不申報的間接對象設定我的錯誤:如果我給它一個二傳手通過只讀屬性的Swift協議可設置屬性
protocol AProtocol {
var name: String { get set }
}
class AnImplementation: AProtocol {
var name = ""
}
class AParent {
var test = AnImplementation()
}
class AChild {
var parent: AParent!
var test: AProtocol {
get { return parent.test }
// Note: Not settable
}
}
var parent = AParent()
var child = AChild()
child.parent = parent
child.test.name = "Hello world!" // Error: Cannot assign to property : 'test' is a get-only property
print(child.test.name)
,它編譯和作品,但它調用setter方法:
protocol AProtocol {
var name: String { get set }
}
class AnImplementation: AProtocol {
var name = ""
}
class AParent {
var test = AnImplementation()
}
class AChild {
var parent: AParent!
var test: AProtocol {
get { return parent.test }
set(newTest) { print("Shouldn't be here!") }
}
}
var parent = AParent()
var child = AChild()
child.parent = parent
child.test.name = "Hello world!"
print(child.test.name)
輸出:
Shouldn't be here!
Hello world!
我不知道我不理解這裏。我想我可以給它一個空的二傳手,但我想了解它的原因。
任何信息非常感謝!
關於值類型setters,請參閱我的答案在這裏例如:http://stackoverflow.com/a/33901138/341994 – matt