On 04/09/2012, at 3:24 PM, Gerriet M. Denkmann <gerr...@mdenkmann.de> wrote:

> I have a class with a mutable array. But from outside it should be just a 
> read-only non-mutable array.
[]

> Is there a better (more elegant) way?


Just return the internal (mutable) array as an NSArray. By typing it as an 
NSArray, you have documented your intent - that the returned value is immutable.

If a client of the code is caching this array, consider making THAT property a 
copy property.

> But when I write:
> self.myStruct.something = 7;
> I get told "Expression is not assignable". (Xcode 4.4.1)

self.myStruct needs to be on the right hand side of an assignment expression, 
not the left. It's equivalent to [self myStruct] which returns a value. x = 
self.myStruct.something is legal, but not self.myStruct.something = x;

but anyway since self.myStruct returns a copy of the struct, assigning some 
value to it is not going to change the copy internal to your class.

A better way to do this is to make each field of the struct a property of the 
object and just set it that way. Why use structs anyway? Make it an object and 
add the intelligence/properties there.

--Graham



_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to