Obj-,当 'self' 时使用的实例变量未设置为 '[(super or self) init...]' 的结果 [英] Obj-, Instance variable used when 'self' is not set to the result of '[(super or self) init...]'
问题描述
我已经问过类似的问题,但我仍然看不到问题?
I asked a similar question to this already, but I still can't see the problem?
-(id)initWithKeyPadType: (int)value
{
[self setKeyPadType:value];
self = [self init];
if( self != nil )
{
//self.intKeyPadType = value;
}
return self;
}
- (id)init {
NSNumberFormatter *formatter = [[[NSNumberFormatter alloc] init]
autorelease];
decimalSymbol = [formatter decimalSeparator];
....
警告来自上面的行 Instance variable used while 'self' is not set to result of '[(super or self) init...]'
推荐答案
您尝试做的在技术上没问题,但在某些阶段您需要调用 [super init]
.如果你的类的 init
方法做了很多其他 initWith...
方法使用的常见初始化,那么把你的 [super init]
放在那里.此外,在尝试使用实例变量之前,请始终确保该类已经init
.
What you are trying to do is technically OK, but at some stage you need to invoke [super init]
. If your class's init
method does a lot of common initialisation that other initWith...
methods utilise, then put your [super init]
in there. Also, always make sure that the class has been init
'd before trying to play around with instance variables.
- (id) initWithKeyPadType: (int)value
{
self = [self init]; // invoke common initialisation
if( self != nil )
{
[self setKeyPadType:value];
}
return self;
}
- (id) init
{
self = [super init]; // invoke NSObject initialisation (or whoever superclass is)
if (!self) return nil;
NSNumberFormatter *formatter = [[[NSNumberFormatter alloc] init]
autorelease];
decimalSymbol = [formatter decimalSeparator];
...
这篇关于Obj-,当 'self' 时使用的实例变量未设置为 '[(super or self) init...]' 的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!