Obj-,当'self'未设置为'[(super或self)init ...]'的结果时使用的实例变量 [英] Obj-, Instance variable used while 'self' is not set to the result of '[(super or self) init...]'

查看:110
本文介绍了Obj-,当'self'未设置为'[(super或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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆