Sam Lee
Sam Lee

Reputation: 10453

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?

Upvotes: 4

Views: 5211

Answers (2)

Barry Wark
Barry Wark

Reputation: 107754

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.

Upvotes: 3

Jesse Rusak
Jesse Rusak

Reputation: 57168

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.)

Upvotes: 10

Related Questions