tags:

views:

370

answers:

6

What is the largest amount of objects I can put in my NSArray?

+1  A: 

Probably more than your RAM can handle.

Eimantas
+4  A: 

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.

Tom
`NSUInteger`, not `unsigned int`. On 64-bit architectures, `unsigned int` is still 4 bytes, whereas `NSUInteger`—which is what NSArray uses since Leopard—is 8.
Peter Hosey
A: 

The memory is allocated when you init the object, so it's limited to the memory free of your device. Moreover the NSArray size is fixed when you init it.

Yannick L.
+1  A: 

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 ...

Joshua Nozzi
+2  A: 

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

Chris S
+2  A: 

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);
}
Dave DeLong