Maximum amount of objects in NSArray
What is the largest amount of objects I can put开发者_开发百科 in my NSArray?
Have you tried to find out? ;)
NSMutableArray * a = [[NSMutableArray alloc] init];
NSUInteger i = 0;
@try {
while (1) {
[a addObject:@"hi"];
i++;
}
} @catch (NSException * e) {
NSLog(@"added %llu elements", i);
}
The NSArray initWithCapacity method, takes an unsigned int as an argument. So whatever the maximum value of an unsigned int is on your platform may be the theoretical limit. However the actual limit is more likely to depend on the amount of memory you have available.
In most cases concerning the upper limits of programming structures and the like:
"If you have to ask, you're probably doing it wrong" - TheDailyWTF.com
Probably more than your RAM can handle.
NSNotFound is defined as NSIntegerMax (this value changes if you are on a 32bit or 64bit system)
NSNotFound is also the result you get when you do a
[nsarray indexOfObject:obj]
and no object is found.
If you do not run out of space/memory I would say that this would be your limit.
http://developer.apple.com/library/mac/#documentation/cocoa/reference/foundation/Miscellaneous/Foundation_Constants/Reference/reference.html
NSArray is a container of pointers to other objects. Its maximum capacity is defined by NSUInteger (on the latest versions of the available OSs):
When building 32-bit applications, NSUInteger is a 32-bit unsigned integer. A 64-bit application treats NSUInteger as a 64-bit unsigned integer
Therefore, whatever the size of NSUInteger is on a given device is the maximum number of object pointers it can contain. However, as Eimantas alluded to in his answer, this isn't the same as "how many objects can it hold" because this depends on available memory. You may not have enough RAM available at a given moment to allocate an array with six billion slots for example ...
精彩评论