Why two arrays?



There something I can’t quite grasp at the beginning of the chapter, in BNRItemStore, why do we declare two array properties? (NSMutableArray privateItems & NSArray allItems)

Why, then, do we create an accessor for allItems that returns privateItems. I guess I am just confused by the fact that we seem to use an NSArray only to display the content of an NSMutableArray. Why not directly and only use the NSMutableArray to store and retrieve the items?

Thank you guys in advance for reading my question.

Best regards,



Two paragraphs in the middle of page 167. Here are some excerpts:

[quote]Because the BNRItemStore wants this kind of control [i.e., adding, removing, and reordering items] over the array, it returns an immutable NSArray to represent the array of items and declares the property as readonly. […]

However, internally, the BNRItemStore needs to be able to mutate the array to add new items (and later remove and order them). This is a pretty common design for a class that wants strict control over its internal data: an object hangs onta a mutable data structure, but other objects only get access to an immutable version of it.
Properties declared in a header file are fair game for users of that class. That’s where the read-only, immutable allItems is declared.

The pay-no-attention-to-the-instance-var-behind-the-curtain mutable privateItems is an implementation detail, and thus not declared in the header.


Thanks for the explanation, much appreciated!