I have a NSMutablearray wich contain NSArrays(each array contain int and String values).
When i try to retrieve and display the data from the first array:
That was ok with the int value, it was displayed correctly.
NSLog(@"%i",[[[lesQuestions objectAtIndex:0] objectAtIndex:0] intValue]);
But when i try to display the String value:
NSLog(@"%@",[[[lesQuestions objectAtIndex:0] objectAtIndex:1] stringValue]);
I got exception:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString stringValue]: unrecognized selector sent to instance
I am definitely sure that the int value is the first item (index 0) and the String value is the second (index 1).
When i log the MutableArray which holds the NSArrays, i got the values correctly, so the problem is on the reference of the item i guess.
The object in your array is already an NSString, so the call to stringValue is unnecessary. NSString doesn’t implement a method called stringValue, hence the exception you’re seeing. Just do this:
-intValueis a method implemented by NSNumber, to get an integer primitive out of the NSNumber instance, and your use ofintValueis correct assuming the first object in the array is an NSNumber (or an NSString, which also implements-intValue).All that said, I don’t generally think it’s a great idea to store instances of different classes in the same array like you’re doing. You’d probably be better off using an NSDictionary where each value is stored with a unique key, say
@"index"for the number and@"name"for the string.