Numbers and mutable sets and retain counts, oh my!
Numbers and mutable sets and retain counts, oh my!
- Subject: Numbers and mutable sets and retain counts, oh my!
- From: William Squires <email@hidden>
- Date: Sat, 13 Nov 2010 08:57:40 -0600
Okay, so I'm getting a bit melodramatic...
Let's say I have some sample code that looks like:
-(NSMutableSet *)allOddNumbersExcept5Set
{
NSMutableSet *results = [[NSMutableSet alloc] initWithCapacity:1];
for (int i=1; i<=10; i+=2)
{
if (i != 5)
{
NSString *testNumber = [NSString stringWithFormat:"%d", i];
// testNumber should now be an autoreleased NSString
[results addObject:testNumber];
}
}
return results;
}
Questions:
1) Do mutable containers (NSMutableArray, NSMutableDictionary,
NSMutableSet) always send the 'retain' message to objects added with
the 'addObject:' message?
2) When you retrieve the results from the collection, does the object
reference you get back have the same retain count as the one in the
container? (i.e. if I add an object to an NSMutableArray (held in an
@property) in one method, and retrieve it in another, will I get a
retain count of 1 or 2?)
3) Will the above code autorelease the 'testNumber' the second (and
third, and fourth...) times through the loop when it gets another
autoreleased NSString, or will it just re-use the existing one?
4) Would I be better off with:
-(NSMutableSet *)allOddNumbersExcept5Set
{
NSMutableSet *results = [[NSMutableSet alloc] initWithCapacity:1];
for (int i=1; i<=10; i+=2)
{
if (i != 5)
{
NSString *testNumber = [[NSString alloc] initWithFormat:@"%d", i];
[results addObject:testNumber];
[testNumber release];
}
}
return results;
}
instead?
_______________________________________________
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