Re: Local Properties
Re: Local Properties
- Subject: Re: Local Properties
- From: David Duncan <email@hidden>
- Date: Mon, 03 Sep 2012 22:35:03 -0700
On Sep 3, 2012, at 10: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.
> My current solution:
>
> MyClass.h file contains:
>
> @property (readonly, nonatomic) NSArray *externalArray;
>
> and MyClass.m file has:
>
> @interface MyClass()
> @property (strong) NSMutableArray *internalArray;
> @end
>
> @implementation MyClass
> - (NSArray *) externalArray { return self.internalArray; }
> @end
>
> Is there a better (more elegant) way?
Not really. Although you may want to return a copy/autoreleased array for -externalArray as otherwise code that uses this accessor would see future mutations to the internalArray.
> And another question:
> I have a struct property like:
> @property (assign) some_Struct_t myStruct;
>
> But when I write:
> self.myStruct.something = 7;
> I get told "Expression is not assignable". (Xcode 4.4.1)
>
> This works:
> some_Struct_t temp = self.myStruct;
> temp.something = 7;
> self.myStruct = temp;
>
> But this is not really nice. There sure must be a better way.
There isn't. You get the message you get because "self.myStruct.something = 7" gets broken down to "[self myStruct].something = 7", but the LHS of that is not assignable (its an R-value, not an L-value, and only L-values are assignable).
--
David Duncan
_______________________________________________
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