Thursday, April 14, 2011

NSMutableArray as @property with readonly

Suppose I have something like this:

@property (readonly) NSMutableArray *someArray;

Can I modify [obj someArray] even though the @property is set to readonly?

From stackoverflow
  • Yes, you can modify its contents. The readonly only applies to the pointer itself - in that way, it is not like C++'s const.

    Basically, saying "readonly" just means "don't translate a.someArray = foo into [a setSomeArray:foo]". That is, no setter is created.

    (Of course, if you wanted to prevent modification, you'd just use an NSArray instead.)

    Matt Gallagher : You mean C's "const". Quick point about C... it depends what side of the asterisk the "const" is on. The readonly property here IS like NSMutableArray * const someArray; but NOT like const NSMutableArray *someArray; http://en.wikipedia.org/wiki/Const-correctness
    Jesse Rusak : @Matt - Good point.
  • The contents of someArray are modifiable, although the property is not (i.e. a call cannot change the value of the someArray instance variable by assigning to the property). Note, this is different from the semantics of C++'s const. If you want the array to be actually read-only (i.e. unmodifiable by the reader), you need to wrap it with a custom accessor. In the @interface (assuming your someArray property)

    @property (readonly) NSArray *readOnlyArray;
    

    and in the @implementation

    @dynamic readOnlyArray;
    
    + (NSSet*)keyPathsForValuesAffectingReadOnlyArray {
      return [NSSet setWithObject:@"someArray"];
    }
    - (NSArray*)readOnlyArray {
      return [[[self someArray] copy] autorelease];
    }
    

    Note that the caller will still be able to mutate the state of objects in the array. If you want to prevent that, you need to make them immutable on insertion or perform a depp-copy of the array in the readOnlyArray accessor.

0 comments:

Post a Comment