On Jun 24, 2016, at 03:12 , Jonathan Mitchell <[email protected]> wrote:
>
> The downside would be having to implement the compliant accessor methods in
> the real model object (in my case the array is a constructed property of an
> NSViewController subclass which obtains its array data from a non native
> Obj-C model)
It’s not much of a downside once you get to this point. You can do it either
the easy way or the good way:
a. If you have a NSMutableArray instance variable with the name of the property
(with or without a leading underscore), the ‘mutableValueForKey:’ mechanism
will access it automatically, and you don’t have to write any code. This
supposes that you did not set ‘accessInstanceVariablesDirectly’ to NO for the
class that has the array property.
b. You can write just the ‘insert…’ and ‘remove…’ accessor methods. The trick
is to define the @property first, and then use autocomplete (typing ‘insert’
and ‘remove’) to get the correct method signature written for you — I can never
remember it without looking it up otherwise. The method implementations can be
as short as 1 line.
For case (b), I also tend to provide an extra piece that hides the
‘mutableValueForKey:’ part. I declare the property this way:
@property (readonly) NSArray* myProperty;
@property (readonly) NSMutableArray* mutableMyProperty; // <— simply
returns ‘[self mutableArrayValueForKey: @“myProperty”]'
Note that these are both readonly, to avoid giving client code accidental
readwrite access to the instance variable that backs the property. However, you
don’t really need this is the access mechanism is via KVC (e.g. bindings). It’s
more of a convenience for client code.
_______________________________________________
Cocoa-dev mailing list ([email protected])
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 [email protected]