JulianB
JulianB

Reputation: 1686

indexOfObject returns 0 first time instead of NSNotFound

I have a table in a UIPopoverController, on viewDidAppear I check for the value of the cell label which is stored in standardUserDefaults. (So I can hilight the last selected option).

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *lastMenu = [prefs objectForKey:@"location"];

NSUInteger index = [__menuTitles indexOfObject:lastMenu];

NSLog(@"lastMenu is %@",lastMenu);
NSLog(@"lastMenu index is %i",index);

Produces this the first time the menu is activated

lastMenu is 
lastMenu index is 0

Subsequent clicks correctly report 2147483647 meaning NSNotFound

lastMenu is 
lastMenu index is 2147483647

Why does 0 get returned the first time???

Here's the array

__menuTitles = [[NSArray alloc] initWithObjects:
                @"North America", 
                @"Western Europe", 
                @"Asia Pacific", 
                @"Latin America", 
                @"Central & Eastern Europe", 
                @"Middle East", 
                @"Africa", 
                nil];

Upvotes: 1

Views: 1910

Answers (1)

Joshua Weinberg
Joshua Weinberg

Reputation: 28688

Likely means that __menuTitles is nil the first time around. Sending a message to nil will result in 0 being returned.

Upvotes: 9

Related Questions