views:

38

answers:

3

Can anyone tell me why this comparison keeps making my app freeze and crash?

NSArray *viewControllerArray = [controlFromMap.navigationController viewControllers];
NSUInteger parentViewControllerIndex = [viewControllerArray count] - 3 // or - whatever;
NSLog(@"Parent view controller: %@", [viewControllerArray objectAtIndex:parentViewControllerIndex]);



if([[[viewControllerArray objectAtIndex:parentViewControllerIndex]stringValue] isEqualToString: @"FromAddressController"]){

    _mapView.showsUserLocation = NO;
}
else{

_mapView.showsUserLocation = YES;
}
+1  A: 

If you add:

NSLog(@"parentViewControllerIndex: %d", parentViewControllerIndex);

after the line:

NSUInteger parentViewControllerIndex = [viewControllerArray count] - 3;

What value do you see in the console?

Alex Reynolds
73793360, that number shows up for that particular stack of controllers
Makinitez21
You're using an `NSUInteger`, which is unsigned. A signed integer that is negative will be wrapped around when it is made unsigned and you'll get a large integer result, like what you see. What this means is that you are getting an out-of-bounds error on the `viewControllerArray` — you are trying to reference an element of this array that does not exist. Perhaps you shouldn't subtract 3 from your count, but something less.
Alex Reynolds
A: 

Also, this line:

if([[[viewControllerArray objectAtIndex:parentViewControllerIndex]stringValue] isEqualToString: @"FromAddressController"]){

looks suspicious to me.

I checked the documentation and the UIViewController class does not appear to have a -stringValue method. So you probably will get an unrecognized selector exception and your application will crash.

Perhaps you mean nibName instead of stringValue?

Alex Reynolds
well, i tried the nibName, but that one did not work cause they are in the same nib file. i guess i have to tlook at something else to compare
Makinitez21
You can look into `class` as Costique suggested, but if you're trying to reference the 73793360th element of `viewControllerArray`, you will certainly run into an out-of-bounds exception that will crash your app, whether you use `class` or `nibName`, because you will not have 73793360+ view controllers on your navigation stack.
Alex Reynolds
right i got u, thats a underflow issue. I'll look deeper into that class. thank you
Makinitez21
+1  A: 

[viewControllerArray objectAtIndex:parentViewControllerIndex] should return an instance of UIViewController subclass. Make sure it responds to -stringValue or (assuming FromAddressController is a class name) make it so:

if([[viewControllerArray objectAtIndex:parentViewControllerIndex] class] == [FromAddressController class])
Costique
right, it does return that value, but from what i see it does not respond to the string value.
Makinitez21