我知道我不久前問過類似的問題,米仍然有點不確定。同樣的事情發生在幾個地方。使用Obj-C,'self'沒有設置爲'[(super或self)init ...]'的結果時使用的實例變量'
實例變量而 '自我' 沒有被設置爲的 '[(超級或自)INIT ...]'
甲
- (id)initWithCoder:(NSCoder *)decoder {
if (![super init]) return nil;
red = [decoder decodeFloatForKey:kRedKey]; //occurs here
green = [decoder decodeFloatForKey:kGreenKey];
blue = [decoder decodeFloatForKey:kBlueKey];
return self;
}
B中的結果
- (id)initWithFrame:(CGRect)frame title:(NSString*)str sideUp:(BOOL)up{
if(![super initWithFrame:frame]) return nil;
int y;
UIImage *img;
if(up){
img = [UIImage imageNamedTK:@"TapkuLibrary.bundle/Images/graph/popup"];
y = 5;
}else{
img = [UIImage imageNamedTK:@"TapkuLibrary.bundle/Images/graph/popdown"];
y = 14;
}
background = [[UIImageView alloc] initWithImage:img]; // occurs here
C
- (id) initWithFrame:(CGRect)frame {
if(![super initWithFrame:frame]) return nil;
UILabel *titleBackground = [[[UILabel alloc] initWithFrame:
CGRectMake(0, 0, 480, 40)] autorelease];
titleBackground.backgroundColor = [UIColor whiteColor];
[self addSubview:titleBackground];
titleLabel = [[UILabel alloc] initWithFrame:CGRectZero]; // occurs here
對於塊A,t他正確
self = [self init];
if(self != nil)
{
和早餐&Ç
- (id) initWithFrame:(CGRect)frame {
super = [super initWithFrame:frame]
if(super != nil)
{
這是個問題嗎?如果是的話,這個問題是什麼? –
如果你寫子類,使用超級 – SAKrisT