views:

71

answers:

2

Hello, here is what i am trying to do:

NSMutableArray *objectNames = [[NSMutableArray alloc] init];
for (Object *o in objectList){
    if (![objectNames containsObject:o.name]) {
        [objectNames addObject:o.name];
    }
}

I am trying to go through an array of objects, then take the objects name (a string) and add it to a string array of objectNames.

This code works in the simulator just fine. but when i run it on the device i get this error.

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSCFArray insertObject:atIndex:]: attempt to insert nil'
+2  A: 

One or more of the objects in objectList has it's name property set to nil. This leads to you trying to insert just nil into objectNames, which gives you the exception.

If it's OK for an object to have a name of nil, what you need to do is to check for this before you insert into objectNames:

NSMutableArray *objectNames = [[NSMutableArray alloc] init];
for (Object *o in objectList){
   if (name && ![objectNames containsObject:o.name]) {
      [objectNames addObject:o.name];
   }
 }
eliego
This works, and I also suggest to make `objectNames` an `NSMutableSet` instead of an array. A set cannot contain duplicates, so you can omit the `-containsObject:` call, plus the fact that it's not indexed, which you don't need here I suppose.
JoostK
Silly mistake thank you, just don't understand why it works on the simulator and not the device. That seems odd to me.
Matt
Perhaps the objects are fetched from some external source (Core Data?), which differs on the device?
eliego
A: 

Looks like one of your Objects doesn't have an name set correctly

Jesse Naugher