Moni Viki
Moni Viki

Reputation: 949

NsMutableArray null value

I have a situation here. I have a UITableviewController with textfields in it in the cell. Each cellthas a textfield in it. The user enters the value in the textfields and i am saving the textfield data in to an NSMutableArray. But if the user doesnot enter a single field then my array gives the error

"CoreAnimation: ignoring exception: * -[NSMutableArray insertObject:atIndex:]: attempt to insert nil object at 2". NSmutableArray cannot have a null value. How will i handle this situation. Any help is appreciated.

Thanks

Upvotes: 1

Views: 2773

Answers (1)

Jason Cragun
Jason Cragun

Reputation: 3233

A couple of quick solutions: I'm assuming that you must have a value in the array at the index that corresponds to the row, so what to put in? Stuff an [NSNull null] object in there, or simply an empty string.

if (!txtField.text)  // if the textfield's text is null
{
    [myMutableArray addObject:[NSNull null]];
}

One issue about this is that when reading your array, you cannot assume that the array holds a string: you must check it for [NSNull null] before setting your text.

Another solution to the issue would be to simply create an NSMutableDictionary to hold the values you wish to store, the key would be the NSIndexPath and the value would be the value you are storing. If the value is null, don't store anything at all. This way you would have no need to store null values.

Upvotes: 7

Related Questions