Re: Local Properties
Re: Local Properties
- Subject: Re: Local Properties
- From: Graham Cox <email@hidden>
- Date: Tue, 04 Sep 2012 15:52:01 +1000
On 04/09/2012, at 3:24 PM, Gerriet M. Denkmann <email@hidden> 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 (email@hidden)
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:
This email sent to email@hidden