Assignment of an ivar not working
I'm seeing a bizarre case where simple assignment to an instance variable is not working. The right hand object is non-nil, but refuses to be assigned to an instance variable (with the same type). This is not happening consistently within the app.
I have a class like this:
@interface FooViewController : UIViewController {
UIView *contentView;
BOOL testBool;
}
@en开发者_运维知识库d
@implementation FooViewController
- (void)viewDidLoad {
UIView *theContentView = [[UIView alloc] initWithFrame:self.view.bounds];
// this assignment fails for some reason:
contentView = [theContentView retain];
/* at this point, contentView == nil */
UIView *bar = theContentView; // this works
testBool = YES; // this works
}
@end
and a subclass like this:
@interface BarViewController : FooViewController {
}
@end
When I instantiate the BarViewController from a nib, when the execution gets to the viewDidLoad defined in the superclass, the strange behavior above happens. Instance variables "refuse to assign".
However, if I add a dummy instance variable to the subclass, the expected behavior returns. Instance variables correctly assign in the viewDidLoad.
I'm seeing this at runtime in iOS 4.3 in the simulator using Xcode 4.0.2. This happens both in LLVM+GCC and GCC.
This is not happening in isolation. When I try this in a test project, it performs normally.
I'm sorry, but I don't believe you.
My first question is how you determine that contentView
is equal to nil? Do you have a breakpoint after the assignment? If you break on the line of the assignment, that line will not yet have been executed, so contentView
will be nil at that point.
If you're testing contentView
in some other method it might just be that viewDidLoad
has not been run yet.
Show me the output of:
- (void)viewDidLoad {
UIView *theContentView = [[UIView alloc] initWithFrame:self.view.bounds];
// this assignment fails for some reason:
contentView = [theContentView retain];
/* at this point, contentView == nil */
NSLog(@"theContentView: %p", theContentView);
NSLog(@"contentView: %p", contentView);
UIView *bar = theContentView; // this works
testBool = YES; // this works
}
If they differ I'll start believing you. (Well, maybe not, I'll still consider the possibility that you're lying.)
精彩评论