Obj-,当'self'未设置为'[(super或self)init ...]'的结果时使用的实例变量 [英] Obj-, Instance variable used while 'self' is not set to the result of '[(super or self) init...]'
问题描述
- (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];
....
警告来自上面的行 'self'未设置为'[(super或self)init ...]'的结果时使用的实例变量
你想做什么是技术上OK,但在某些阶段你需要调用 [super init]
。如果你的类的 init
方法使用了很多其他 initWith ...
方法的常见初始化, code> [super init] 。此外,在尝试使用实例变量之前,始终确保类已经 init
'。
- (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];
...
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];
....
The warning comes from the line above Instance variable used while 'self' is not set to the result of '[(super or self) 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或self)init ...]'的结果时使用的实例变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!