It sure seems like the Xcode debugger has been playing a dirty trick on me for the last couple hours.
I was working with this code which has--in hindsight--a pretty obvious bug, but it wasn't obvious at the time:
- (UIImage*)loadBundleImage:(NSString*)imageName {
NSBundle* bundle = [NSBundle mainBundle];
NSString* path = [bundle bundlePath];
path = [path stringByAppendingFormat:@"%@/%@", path, imageName];
return [UIImage imageWithContentsOfFile:path];
}
I was using the debugger to step through the code, and it kept saying that bundle was nil after the [NSBundle mainBundle] call. This led me to search high and low for why that could possibly be and to try to fix that problem.
Ultimately, I found the actual bug (which was that I was appending the path twice), so I decided to do some experimenting... obviously, bundle wasn't nil, so why was the debugger saying it was? When stepping through this code, the debugger correctly shows the value of bundle after the mainBundle call:
- (UIImage*)loadBundleImage:(NSString*)imageName {
NSBundle* bundle = [NSBundle mainBundle];
if (bundle == nil) {
NSLog(@"Nil bundle");
}
NSString* path = [bundle bundlePath];
path = [path stringByAppendingFormat:@"/%@",imageName];
return [UIImage imageWithContentsOfFile:path];
}
So, what gives here? Is it a bug with the debugger? My only wild guess is that the compiler is optimizing the consecutive calls to mainBundle and bundlePath into some sort of atomic-ish operation, so the debugger doesn't get to "see" what happened... but that when I broke those two calls up with the if-block... that forced the compiler to deal with them separately?!
Can anyone shed some light on this? Am I as crazy as I feel? Please tell me I'm wrong and that I can go back to trusting the debugger.
Thanks, sorry for the mini-rant, there.