Setting a default value other than 0 for a property

10,451

Solution 1

Set it to -1 in your designated initializer.

EDIT:
In response to your question, a designated initializer is the one initializer which must always be called when your class instance is created. It is the only place where [super init]; is called (which initializes the part of the class that you inherit from your parent class), and all other initializers must call this designated initializer in order to make sure that the class is properly setup and all default values are set, including those that you want to set.

In your case, if you have no other methods that begin with init, then you should do it in the -(id)init method. As some other people have shown since I posted my answer, it would look like this:

-(id)init {
    if (self = [super init]) {
        _stackTop = -1;
    }
    return self;
}

Solution 2

You should set it in the designated initializer. For example, if your designated initializer is parameterless, you can do it like this:

-(id)init {
    if (self = [super init]) {
        _stackTop = -1;
    }
    return self;
}

Solution 3

Set it in your init methods.

e.g.

-(id)init
{
 self = [super init];
 if(self)
 {
   _stackTop = -1;
 }
 return self;
}

Solution 4

In your init method for the class you've created (or are using), add the line:

self.stackTop = -1;

That will initialize your property when the object is created.

Then, you can get rid of your custom setter method.

Share:
10,451
Hammad
Author by

Hammad

Updated on June 04, 2022

Comments

  • Hammad
    Hammad almost 2 years

    I have an int property

    @property(nonatomic) int stackTop;
    

    and when it gets synthesized, it has a default value of 0. I want to set this default value to -1.

    I tried overriding the setter method like this

    @sythesize stackTop = _stackTop;
    
    -(void)setStackTop:(int)stackTop
    {
        _stackTop = -1;
    }
    

    and then the initial value is -1 instead of 0, but it's not changing when the variable stackTop changes in the program. For example, I have self.stackTop++;, but it's not getting incremented. With the non-overriden setter method, that statement does change the value. What am I doing wrong? What's the solution?

  • Hammad
    Hammad almost 12 years
    where is that and what is that
  • David V
    David V almost 12 years
    Those are your initialization methods. These are called when your code calls [[MyClass alloc] init];. They start with the text init. If you don't see any methods in your class (or base classes) starting with init then you are using the default initializer. The default initializer is the -(id)init method. In that case you can copy and paste any of the code snippets from these answers. You really should read up on class initializers in Objective C though.
  • Lukasz 'Severiaan' Grela
    Lukasz 'Severiaan' Grela about 10 years
    -1 as init method returns something and you have skipped it. It should have return self;
  • Lukasz 'Severiaan' Grela
    Lukasz 'Severiaan' Grela about 10 years
    -1 as init method returns something and you have skipped it. It should have return self;
  • Lukasz 'Severiaan' Grela
    Lukasz 'Severiaan' Grela about 10 years
    Possibly but from a person with 237k I would expect more:)
  • Lukasz 'Severiaan' Grela
    Lukasz 'Severiaan' Grela about 10 years
    40k is still large enough, but to cut the score auction - I've felt that it was right to down-vote and I've gave reason - not to discuss it but to notify why down-voted.
  • Sergey Kalinichenko
    Sergey Kalinichenko about 10 years
    @Lukasz'Severiaan'Grela There's no "score auction" here, it has been over for quite a while. You downvoted all but one answers on a page that's nearly two years old, so the downvotes have zero effect. It's just that after my edits to all three answers that you downvoted, your comments no longer look relevant.