• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: indexed accessor properties and NSArray/NSMutableArray's
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


  • Follow-Ups:
    • Re: indexed accessor properties and NSArray/NSMutableArray's
      • From: Roland King <email@hidden>
References: 
 >indexed accessor properties and NSArray/NSMutableArray's (From: Roland King <email@hidden>)
 >Re: indexed accessor properties and NSArray/NSMutableArray's (From: Jeff Johnson <email@hidden>)
 >Re: indexed accessor properties and NSArray/NSMutableArray's (From: Roland King <email@hidden>)

  • Prev by Date: Re: indexed accessor properties and NSArray/NSMutableArray's
  • Next by Date: Re: indexed accessor properties and NSArray/NSMutableArray's
  • Previous by thread: Re: indexed accessor properties and NSArray/NSMutableArray's
  • Next by thread: Re: indexed accessor properties and NSArray/NSMutableArray's
  • Index(es):
    • Date
    • Thread