Re: Limited-access, KVO-compliant mutable array ?,

  • On Jul 11, 2012, at 7:34 AM, <cocoa-dev-request...> wrote:

    > I still disagree, in the sense that, as a client, I would never expect the contents of an NSArray to change (only the array assigned to the property). In practice, that may not matter. In any case, I can live with it.

    As Ken Thomases said, this expectation is unfounded. Think of it this way: you can have the same expectations of a NSArray as you have with a standard C array reference. This is, of course, an extremely simplified statement but to go along with it here are some equally simplified examples:

    void * someArrayReference = someObject.arrayReference();
    NSArray * someNSArray = [someObject array];

    Ignoring threading and related issues, you can expect that the reference (pointer) to the array will not change and that the contents of the array will not changeā€¦ as long as you don't do anything that may allow something else to change. A very simple example of such actions would be:

    someObject.addUser( someUser );    // where someArrayReference is a reference to the user array

    And of course you wouldn't expect that either the reference or the array contents would not change after exiting the method where you retrieved the reference. In other words, each call to someObject.arrayReference() may return a different array reference or for the data in the array to have changed since the last call.

    As with anything, you can over think this. Even my simplified explanation here is really over done; simply put:

    An NSArray reference and it's contents can be expected to never change within the scope of a single use, but not between uses.

    As far as the implementation goes, I use William Squires' method of implementing this pattern (public read-only NSArray property that refers to the private read-write NSMutableArray property); it's very simple and provides exactly what you are looking for (if not exactly what you are expecting).

    -lane
previous month july 2012 next month
MTWTFSS
            1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31          
Go to today