我看到一個奇怪的情況,其中一個實例變量的簡單賦值不起作用。右手對象非零,但拒絕分配給實例變量(具有相同類型)。這在應用程序中並不一致。分配一個不工作的ivar
我有這樣一個類:
@interface FooViewController : UIViewController {
UIView *contentView;
BOOL testBool;
}
@end
@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
和這樣的一個子類:
@interface BarViewController : FooViewController {
}
@end
當我從筆尖,實例化BarViewController在當執行獲取到viewDidLoad中定義超類,上述奇怪的行爲發生。實例變量「拒絕分配」。
但是,如果我將一個虛擬實例變量添加到子類,則預期的行爲將返回。實例變量在viewDidLoad中正確分配。
我在使用Xcode 4.0.2的模擬器中在iOS 4.3的運行時看到了這個。這在LLVM + GCC和GCC中都會發生。
這不是孤立地發生。當我在一個測試項目中嘗試這個時,它正常運行。
爲什麼不只是'contentView = [[UIView alloc] initWithFrame:[[self view] bounds]];'? – 2011-05-19 05:07:00
你確定viewDidLoad被調用?您是否已經在調試器中逐步完成了任務,以查看ContentView和contentView的值? – sbooth 2011-05-19 05:07:40
@dave是的,我可以做到這一點,但潛在的問題仍然存在。我在這個例子中簡化了代碼。即使添加ivar專門用於調試和分配它不起作用。這就像ivars根本就沒有分配任務。 – 2011-05-19 05:17:14