Re: indexed accessor properties and NSArray/NSMutableArray's
Re: indexed accessor properties and NSArray/NSMutableArray's
- Subject: Re: indexed accessor properties and NSArray/NSMutableArray's
- From: Jeff Johnson <email@hidden>
- Date: Tue, 5 Aug 2008 12:10:36 -0500
On Aug 5, 2008, at 11:56 AM, Roland King wrote:
On Aug 5, 2008, at 11:50 PM, Jeff Johnson wrote:
Roland,
How are you mutating your array? If you do [[myObject
mutableArrayValueForKey:@"myArray"] addObject:anObject], it should
preserve the current NSMutableArray even without indexed
accessors. This seems to work in my testing, at least.
-Jeff
I don't get that in my testing. As trivial an example as I can make
below, Foo, one property, prop, it's a NSMutableArray*, has a
getter and a setter. The main() function calls
[ [ foo mutableArrayValueForKey:@"prop" ] addObject:@"something" ]
and that replaces the array by calling setProp:, doesn't mutate the
one there.
Only when I add both (not just the insert, but both insert and
remove) of the mutator methods, will access via the
mutableArrayForKey call insertObject:inPropAtIndex:
@interface Foo : NSObject {
NSMutableArray *prop;
}
-(id)init;
-(void)dealloc;
-(NSMutableArray*)prop;
-(void)setProp:(NSMutableArray*)propVal;
@end
@implementation Foo
-(id)init
{
self = [ super init ];
if( self )
{
prop = [ [ NSMutableArray alloc ] init ];
}
return self;
}
-(void)dealloc
{
[ prop dealloc ];
[ super dealloc ];
}
-(NSMutableArray*)prop
{
return prop;
}
-(void)setProp:(NSMutableArray*)propVal
{
[ propVal retain ];
[ prop release ];
prop = propVal;
}
@end
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
Foo *foo = [ [ Foo alloc ] init ];
[[ foo mutableArrayValueForKey:@"prop" ] addObject:@"Another
String" ];
[pool drain];
return 0;
}
Roland,
Right, that's because you've defined a setter. See the search
algorithm documented for mutableArrayValueForKey: at <http://
developer.apple.com/documentation/Cocoa/Reference/Foundation/
Protocols/NSKeyValueCoding_Protocol/Reference/Reference.html#//
apple_ref/doc/uid/20000471-BABJFEAE>. Try removing the setter.
-Jeff
_______________________________________________
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