Reputation: 724
I have an NSArray (RSIatAddr), and I want to check whether the object at the given index = 0 (or some other arbitrary constant) but the following code gives me no warnings / error ; but is throwing an exception
if ([[RSIatAddr objectAtIndex:j] isEqualToNumber:nil]) {
[sumRSI addObject:[NSNumber numberWithInt:[[RSIatAddr objectAtIndex:j]intValue]*
[[sumRSI objectAtIndex:j] intValue]]]; }
The error I'm getting:
2012-10-02 00:42:00.266 NetSearch+DetectLocation[3741:707]
*** Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: '-[__NSCFNumber compare:]: nil argument'
Upvotes: 0
Views: 184
Reputation: 191
Without seeing the code that actually fills your array, its likely the array is holding a different type than you are expecting. Try setting a breakpoint right before that line of code, and typing "po [[RSIatAddr objectAtIndex:j] class]" and see what is actually there, then you can also try your isEqualToNumber in the debug window to see if that succeeds.
I just implemented this in the AppDelegate of a new iOS App and it works:
NSArray *array = [[NSArray alloc] initWithObjects:[NSNumber numberWithInt:0], [NSNumber numberWithInt:1], nil];
NSLog(@"%@", array);
for (NSNumber *num in array) {
if ([num isEqualToNumber:[NSNumber numberWithInt:0]]) {
NSLog(@"Equal");
} else {
NSLog(@"Not Equal");
}
}
Upvotes: 1
Reputation: 64002
isEqualToNumber:
takes an NSNumber
as an argument, not the integer that you're passing.
The integer that you're using, 0
, happens to be the value of nil
, but any integer would be extremely unlikely to be a valid pointer value for an NSNumber
, and would also therefore crash.
Upvotes: 3