ios - 修改对象的内部属性时,仅获取协议(protocol)中定义的属性会导致编译错误
<p><p>考虑这样的代码:</p>
<pre><code>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
</code></pre>
<p>此代码编译时出错:</p>
<pre><code>error: cannot assign to property: 'myProperty' is a get-only property
o.myProperty.something = true
~~~~~~~~~~~~ ^
</code></pre>
<p>为什么?我的属性是 SomeProtocolImplementation 类型,它是类类型,因此应该可以使用对 myProperty 的引用来修改它的内部属性。 </p>
<p>更进一步,修改 myProperty 定义使其看起来像这样:</p>
<pre><code>var myProperty: SomeProtocol { get set }
</code></pre>
<p>发生了一些奇怪的事情。现在代码编译(并不奇怪),但输出是:</p>
<pre><code>something changed!
myProperty has changed
</code></pre>
<p>所以此时 SomeProtocolImplementation 开始表现得像一个值类型 - 修改它的内部状态会导致触发 myProperty 的“didSet”回调。就像 SomeProtocolImplementation 是 struct ... </p>
<p>我实际上找到了解决方案,但我也想了解发生了什么。解决方法是将 SomeProtocol 定义修改为:</p>
<pre><code>protocol SomeProtocol: class {
var something: Bool { get set }
}
</code></pre>
<p>它工作正常,但我试图理解它为什么会这样。谁能解释一下?</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>首先阅读 <a href="https://developer.apple.com/library/prerelease/content/documentation/Swift/Conceptual/Swift_Programming_Language/Protocols.html#//apple_ref/doc/uid/TP40014097-CH25-ID281" rel="noreferrer noopener nofollow">Class Only Protocol</a>是。专注于注释部分:</p>
<blockquote>
<p>Use a class-only protocol when the behavior defined by that protocol’s requirements assumes or requires that a conforming type has <strong>reference semantics</strong> rather than value semantics.</p>
</blockquote>
<p>上面的引用应该会让你明白。</p>
<p>您正试图为您的 <code>SomeProtocol</code> 的符合类(即 <code>SomeProtocolImplementation</code>)获取 <strong>reference type</strong> 的行为。您希望将来能够更改 <code>something</code> 的值。所以基本上你是指向<strong><em>上面引用的</em></strong>句子。</p>
<p>如果您需要更多说明,请考虑以下更有意义的设计,为方便起见,我更改了命名:</p>
<pre><code>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
</code></pre></p>
<p style="font-size: 20px;">关于ios - 修改对象的内部属性时,仅获取协议(protocol)中定义的属性会导致编译错误,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/45836642/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/45836642/
</a>
</p>
页:
[1]