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

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

警告来自上方的行未将'self'设置为'[((super or self)init ...] init ...]'的结果时使用的实例变量

推荐答案

从技术上来说,您想要做的是可以的,但是在某些情况下,您需要调用 [super init] 。如果您的类的 init 方法执行了其他 initWith ... 方法利用的许多常见初始化,则将您的 [超级初始化] 在那里。另外,在尝试使用实例变量之前,请始终确保已对该类进行了 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天全站免登陆