考虑这样的代码:
protocol SomeProtocol {
var something: Bool { get set }
}
class SomeProtocolImplementation: SomeProtocol {
var something: Bool = false {
didSet {
print("something changed!")
}
}
}
protocol MyProtocol {
var myProperty: SomeProtocol { get }
}
class MyClass: MyProtocol {
var myProperty: SomeProtocol = SomeProtocolImplementation() {
didSet {
print("myProperty has changed")
}
}
}
var o: MyProtocol = MyClass()
o.myProperty.something = true
此代码编译时出错:
error: cannot assign to property: 'myProperty' is a get-only property
o.myProperty.something = true
~~~~~~~~~~~~ ^
为什么?我的属性是 SomeProtocolImplementation 类型,它是类类型,因此应该可以使用对 myProperty 的引用来修改它的内部属性。
更进一步,修改 myProperty 定义使其看起来像这样:
var myProperty: SomeProtocol { get set }
发生了一些奇怪的事情。现在代码编译(并不奇怪),但输出是:
something changed!
myProperty has changed
所以此时 SomeProtocolImplementation 开始表现得像一个值类型 - 修改它的内部状态会导致触发 myProperty 的“didSet”回调。就像 SomeProtocolImplementation 是 struct ...
我实际上找到了解决方案,但我也想了解发生了什么。解决方法是将 SomeProtocol 定义修改为:
protocol SomeProtocol: class {
var something: Bool { get set }
}
它工作正常,但我试图理解它为什么会这样。谁能解释一下?
首先阅读 Class Only Protocol是。专注于注释部分:
Use a class-only protocol when the behavior defined by that protocol’s requirements assumes or requires that a conforming type has reference semantics rather than value semantics.
上面的引用应该会让你明白。
您正试图为您的 SomeProtocol
的符合类(即 SomeProtocolImplementation
)获取 reference type 的行为。您希望将来能够更改 something
的值。所以基本上你是指向上面引用的句子。
如果您需要更多说明,请考虑以下更有意义的设计,为方便起见,我更改了命名:
protocol Base: class {
var referenceTypeProperty: Bool { get set }
// By now you are assuming: this property should be modifiable from any reference.
// So, instantly make the protocol `Class-only`
}
class BaseImplementation: Base {
var referenceTypeProperty: Bool = false {
didSet {
print("referenceTypeProperty did set")
}
}
}
protocol Child {
var valueTypeProperty: Base { get }
// This property shouldn't be modifiable from anywhere.
// So, you don't need to declare the protocol as Class-only
}
class ChildImplementation: Child {
var valueTypeProperty: Base = BaseImplementation() {
didSet {
print("valueTypeProperty did set")
}
}
}
let object: Child = ChildImplementation()
object.valueTypeProperty.referenceTypeProperty = true
关于ios - 修改对象的内部属性时,仅获取协议(protocol)中定义的属性会导致编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45836642/
欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://jike.in/) | Powered by Discuz! X3.4 |