Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The collection accessor patterns that you were reading about improve the way key value coding works with collections like NSArray, NSDictionary, etc. If you implement them in your own classes, your classes can be used with KVC just like the standard collection classes. For example, maybe you've got a <code>Flight</code> class that contains a list of <code>Passenger</code>. But maybe those passengers aren't stored in an NSArray, but pulled from an database or something. If you implement the following methods:</p> <ul> <li><code>-countOfPassengers</code></li> <li><code>-passengersAtIndexes:</code></li> <li><code>-getPassengers:range:</code></li> </ul> <p>then code using your class can use KVC to efficiently access the passenger list. For example, you might get a list of passenger frequent flyer numbers (assuming <code>Passenger</code> has such a property) like this:</p> <pre><code>NSArray *frequentFlyerNumbers = someFlight.passengers.frequentFlyerNumbers; </code></pre> <p>That's pretty handy stuff -- saves you from having to iterate over the collection and collect all those numbers yourself. If you weren't aware that you could do that with the standard collections like NSArray, this might seems surprising, so take a minute to absorb that part first and then consider that by implementing a few simple methods you've done the same thing for your own <code>Flight</code> class. It's pretty cool that this works even though the storage of the passengers is entirely internal to your class.</p> <p>Now, the methods above only cover getters, but you can do the same for setters by implementing:</p> <ul> <li><code>-insertPassengers:atIndexes:</code></li> <li><code>-removePassengersAtIndexes:</code></li> <li><code>-replacePassengersAtIndexes:withPassengers:</code></li> </ul> <p>Now, again, your code can add and remove passengers from a flight using KVC:</p> <pre><code>[someFlight replacePassengersAtIndexes:bumpedPassengerIndexes withPassengers:passengersFromLateConnectingFlight]; </code></pre> <p>There are other KVC accessors that you can implement, too, but I don't think I need to list them all for you since they're right there in the article that you linked. The point is that key value coding is useful stuff that's used in a number of places, and by implementing a small handful of methods you can make your own classes KVC compliant.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

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