Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSDictionary and retaining values



It should be 1 since you didn't alloc or manually retain kArr. -arrayWithObjects autoreleases the created array. This is true across the Cocoa framework: helper object creation methods always autorelease unless otherwise annotated in the docs.

e.g.:

[[NString alloc]init] ; // retains

NSString *aString = [NSString stringWithString:@"test"] ; // does not retain (or, more correctly, retains and then autoreleases)
[aString retain] ; // now it's retained

NSString *aString = [NSString stringWithString:@"test"] ; // does not retain (or, more correctly, retains and then autoreleases)
[anArray addObject:aString] ; // now it's retained

Mike


On Apr 18, 2008, at 10:58 AM, Optical Ali wrote:
Hi,

Does NSDictionary retain the objects (values) that are added?
Shouldn't "n" be 2 below?

Thanks.

int main(int argc, char *argv[])
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    

NSMutableArray *kArr = [NSMutableArray arrayWithObjects:@"1", @"2", @"3", @"4", nil];
NSMutableArray *aArr = [NSMutableArray arrayWithObjects:@"2", nil];
NSDictionary *aDic = [NSDictionary dictionaryWithObjectsAndKeys:
@"K", kArr, @"A", aArr, nil];


int n = [kArr retainCount];
// Shouldn't n be 2 here?
        [pool release];
         return 0;
}

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Objc-language mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/objc-language/email@hidden

This email sent to email@hidden

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Objc-language mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/objc-language/email@hidden

This email sent to email@hidden

References: 
 >NSDictionary and retaining values (From: "Optical Ali" <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.