Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Yes.</p> <blockquote> <p>There is however one aspect which still leaves me puzzled: how to properly model to-many relationships, especially when the collection is to be publicly immutable, but privately mutable ….</p> </blockquote> <p>Easy: Declare the property as <code>readonly</code> in the header, then <a href="http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC/Articles/ocProperties.html#//apple_ref/doc/uid/TP30001163-CH17-SW19" rel="nofollow noreferrer">redeclare it as <code>readwrite, copy</code> in a class extension</a> in the implementation file.</p> <blockquote> <p>I do understand how to implement the KVC accessor methods for to-many relationships (<code>countOf&lt;Key&gt;</code>, <code>objectsIn&lt;Key&gt;AtIndex</code>, etc.) and this is the route I've been following so far.</p> </blockquote> <p>There are mutative ones, too. With these, you don't need to use <code>mutableArrayValueForKey:</code>; instead, you can use the mutative accessors directly. You'll still get KVO notifications, because KVO wraps those methods the first time something adds itself as an observer for the property.</p> <p>I have <a href="http://boredzo.org/blog/archives/2007-08-07/a-complete-raw-list-of-kvc-accessor-selector-formats" rel="nofollow noreferrer">a list of the accessor selector formats</a>, including the mutative accessors, on my blog.</p> <p>Edit:</p> <blockquote> <p>Even when I declare a to-many property as readonly, like in the example above, external code can still call <code>mutableArrayValueForKey:@"transactions"</code> on the model object and mutate the collection.</p> </blockquote> <p>This is a good reason to make it a habit to use the mutative accessors and avoid <code>mutableArrayValueForKey:</code>. You won't send mutation messages from outside the class if you get a compiler warning (no such [public] method) any time you try it.</p> <p>Despite the availability of <code>mutableArrayValueForKey:</code> and the risk that someone will use it, KVO-compliant properties <em>are</em> the way to go here.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload