#import <Foundation/NSObject.h>
#import <Foundation/NSAutoreleasePool.h>
#import <Foundation/NSString.h>
#import <Foundation/NSArray.h>
#import <Foundation/NSData.h>
#define ARRAY_CAPACITY 10
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSMutableArray * myArr = [NSMutableArray arrayWithCapacity: ARRAY_CAPACITY];
NSNumber *first_number, *replacement_number;
first_number = [NSNumber numberWithInt: 42];
replacement_number = [NSNumber numberWithInt: 17];
NSLog(@"Retain Count: \"First Number:\" = %i", [first_number retainCount]);
NSLog(@"Retain Count: \"Replacement Number:\" = %i", [replacement_number retainCount]);
[myArr addObject: first_number];
NSLog(@"Add \"First Number\" to array");
NSLog(@"Retain Count: \"First Number:\" = %i", [first_number retainCount]);
NSLog(@"Retain Count: \"Replacement Number:\" = %i", [replacement_number retainCount]);
NSLog(@"Now replace \"First Number\" with \"Replacement Number\"");
[myArr replaceObjectAtIndex:myArr.count - 1 withObject:replacement_number];
NSLog(@"Retain Count: \"First Number:\" = %i", [first_number retainCount]);
NSLog(@"Retain Count: \"Replacement Number:\" = %i", [replacement_number retainCount]);
NSLog(@"\"Like NSArray, instances of NSMutableArray maintain strong references to their contents. If you do not use garbage collection, when you add an object to an array, the object receives a retain message. When an object is removed from a mutable array, it receives a release message. If there are no further references to the object, this means that the object is deallocated.\" My comment: I imagine that \"replace\" is nothing more than \"remove\" and \"addObject\" and thus the result is to be expected.");
[pool drain];
return 0;
}
Note: if you use numbers from -1 through 12 you will get "strange" retainCount numbers.
From Steve.
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];;
NSNumber *first_number;
NSNumber *n1, *n2, *n3, *n4, *n5;
first_number = [NSNumber numberWithInt: 42];
n1 = [NSNumber numberWithInt: 7];
n2 = [NSNumber numberWithInt: 7];
n3 = [NSNumber numberWithInt: 10];
n4 = [NSNumber numberWithInt: 42];
NSLog (@"%i %i %i %i %i", [first_number retainCount], [n1 retainCount],
[n2 retainCount], [n3 retainCount], [n4 retainCount]);
[pool drain];
}Output: 1 3 3 2 1
So the retain count for the number 42, even though repeated, is still 1. The repeat count for 10, which appears once, is 10, and the retain count for 7, which appears twice is 3.
I found a discussion on the Internet that the numbers -1 to 12 are used so often that they're "pre-generated" and then is the number is used in the program, the compiler will use the pre-generated number and bump its retain count by 1. So, for a number from -1 to 12, its retain count will be n + 1 if it's used n times in the program (7 is used twice, so its retain count is 2 + 1 = 3). The first retain count of 1 comes from the original pre-generated object Note that this happens at compile time: the compiler sees the reference to the pre-generated (or "cached") number object, creates a reference to that object, and appropriately sets its retain count to the total number of times it's used plus 1. Also note that 42 is used twice, but since it's not in the range of -1 to 12 a new number object gets created each time and thus each object has a retain count of 1.
BTW....here is the ref to the above.
http://developer.apple.com/releasenotes/Cocoa/FoundationOlder.html (thanks to Eddietr of macrumors)