Obj-, the Instance variable used during "self" is not set by the result "[(super or self) init ...] '

I already asked a similar question, but I still do not 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];
....

A warning is displayed from the line above. Instance variable used while 'self' is not set to the result of '[(super or self) init...]'

+5
source share
2 answers

What you are trying to do is technically good, but at some point you need to call [super init]. If your class method initdoes a lot of the general initialization used by other methods initWith..., then put it there [super init]. Also, always make sure the class was initd before trying to play 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];

    ...
+4

, . - decimalSymbol, , .

self = [super init];

init. - , - NSObject ( ).

+2

All Articles