考虑这样的代码:
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 将是结构化的...
我实际上找到了解决方案,但我也想了解发生了什么。解决方案是将 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
)获取引用类型的行为。您希望将来能够更改 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/