C/Java/etc. style arrays in Objective-C

Go To StackoverFlow.com


I know Apple is big on having you use NS objects instead of true primitive types, but I need the capabilities of an array (namely direct access of items at indices). However, it seems that they are so very keen on using NS objects that I can't find a single tutorial online or in a textbook about how to use basic primitive arrays. I want something that does things like this does in Java:

String inventory[] = new String[45];

inventory[5] = "Pickaxe";
inventory[12] = "Dirt";
inventory[8] = "Cobblestone";

inventory[12] = null;

System.out.println("There are " + inventory.length + " slots in inventory: " + java.util.Arrays.toString(inventory));

The following is the closest I've gotten in Objective-C, but it won't run properly:

NSString *inventory[45];

inventory[5] = @"Pickaxe";
inventory[12] = @"Dirt";
inventory[8] = @"Cobblestone";

inventory[12] = nil;

NSArray *temp = [NSArray arrayWithObjects:inventory count:45];
NSLog(@"There are %i slots in inventory: %@", [temp count], [temp description]);

Also, if at all possible, is there something in O-C that will give me the count of non-null/non-nil objects in the array? (This way, I can tell how much space is left in the inventory so that the player can't pack away anything if it's full)

2012-04-04 04:48
by Supuhstar
It's natural that it doesn't work. [NSArray arrayWithObjects:inventory]; would have required that inventory be a list of variadic arguments. User [NSArray arrayWithObjects:count:] instead - NoName 2012-04-04 05:01
Don't forget that objective-c is built on top of C so any standard C code will work. Just search for an example of a C array if you aren't sure how to use one - lnafziger 2012-04-04 05:08
@H2CO3 thanks for that! However, it still throws an exception for putting a nil value in ther - Supuhstar 2012-04-04 05:08
That's your fault anyway! Collection objects do throw exceptions upon being given nil, and this behaviour is documented. You should check for nil before passing it to the initializer methods - NoName 2012-04-04 13:48
ugh... Why, Apple? Why.. - Supuhstar 2012-04-04 15:00


typically, you would use NSArray/NSMutableArray, although you could also use C arrays.

NSArray (and most Foundation collections) cannot contain nil entries - you can use NSPointerArray (OS X) if you need nil values, or simply use [NSNull null] in an NSArray to designate nil.

Here's your program using an NSArray:

NSMutableArray * inventory = [NSMutableArray array];
for (NSUInteger idx = 0; idx < 45; ++idx) {
  [inventory addObject:[NSNull null]];

[inventory replaceObjectAtIndex:5 withObject:@"Pickaxe"];
[inventory replaceObjectAtIndex:12 withObject:@"Dirt"];
[inventory replaceObjectAtIndex:8 withObject:@"Cobblestone"];

[inventory replaceObjectAtIndex:12 withObject:[NSNull null]];

NSArray *temp = [NSArray arrayWithObject:inventory];
NSLog(@"There are %i slots in inventory: %@", [temp count], [temp description]);

Also, if at all possible, is there something in O-C that will give me the count of non-null/non-nil objects in the array?

An NSArray is a CFArray - they are "toll free bridged". Use CFArrayGetCountOfValue with [NSNull null] as the value to seek. Most of the CF-APIs are available in the NS-APIs, but not this one. You could easily wrap it in a category, if needed often.

2012-04-04 04:57
by justin
when it tries to make the NSArray, it says Program received signal: EXC_BAD_ACCESS. and exits - Supuhstar 2012-04-04 05:15
@Supuhstar ah - i missed that. updated. that was taken from the original. you want arrayWithObject:, not arrayWithObjects: there. you need to nil-terminate arrayWithObjects:, like so: NSArray *temp = [NSArray arrayWithObjects:inventory, nil];justin 2012-04-04 05:24
awesome! I'm going to use this system, now : - Supuhstar 2012-04-04 14:59


Use c-style arrays. Any c code will work in objective c. Try not to mix objective c style objects with c style code unless you are very careful. The memory management stuff gets weird, and it is easy to get confused on object types vs. primitives.

2012-04-04 04:54
by Adam Shiemke
Can I have some code samples? I was never formally taught C - Supuhstar 2012-04-04 04:57
... so go read a book, or google "C array tutorial"? SO isn't the place to get people to personally teach you material that you can easily research yourself - Kurt Revis 2012-04-04 05:00
I just wanted a snippet, jeeze >.> If someone asked me for a sample of Java, HTML, CSS, VB, etc. code, I'd gladly write it out - Supuhstar 2012-04-04 05:05