加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

objective-c – Obj-,’self’时使用的实例变量未设置为'[(s

发布时间:2020-12-14 17:56:10 所属栏目:百科 来源:网络整理
导读:我已经问了一个类似的问题,但我仍然看不出问题? -(id)initWithKeyPadType: (int)value{ [self setKeyPadType:value]; self = [self init]; if( self != nil ) { //self.intKeyPadType = value; } return self;}- (id)init { NSNumberFormatter *formatter =
我已经问了一个类似的问题,但我仍然看不出问题?

-(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 …]的结果

解决方法

你要做的是技术上没问题,但在某个阶段你需要调用[super init].如果你的类的init方法执行了许多其他initWith …方法使用的常见初始化,那么将你的[super 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];

    ...

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读