1

When coding my iPhone app. Is it always a good practice when setting or getting values to use self? I seem to forget half of the time and recently have been tracking down a bug I believe is related to this.

Should I ALWAYS use self or are there cases when it's not necessary or would cause problems?

Edit: Here's an example of what I'm doing in the code

else if([CellIdentifier isEqualToString:@"Linked Item"]) {
        linkedItemLabel = [[UILabel alloc] initWithFrame:CGRectMake(120, 5, 160, 34)];
        linkedItemLabel.adjustsFontSizeToFitWidth = YES;
        linkedItemLabel.textColor = [UIColor blackColor];
        linkedItemLabel.font = [UIFont systemFontOfSize:17.0];
        linkedItemLabel.text = [storedValuesMutableArray objectAtIndex:7];
        linkedItemLabel.backgroundColor = [UIColor clearColor];
        linkedItemLabel.textAlignment = UITextAlignmentRight;
        [cell addSubview:linkedItemLabel];}

This is part of the code that sets up my tableviewcells for a form that needs to be filled. Should I be using self.linkedItemLabel or is this fine?

Hackmodford
  • 3,901
  • 4
  • 35
  • 78
  • [Property vs. instance variable](http://stackoverflow.com/questions/719788/property-vs-instance-variable) and [Properties and Instance Variables in Objective-C 2.0](http://stackoverflow.com/questions/3074248/properties-and-instance-variables-in-objective-c-2-0) and [What is the difference between ivars and properties in Objective-C](http://stackoverflow.com/questions/4172810/what-is-the-difference-between-ivars-and-properties-in-objective-c) – beryllium Dec 01 '11 at 15:58
  • possible duplicate of [When to call self.myObject vs just calling myObject in Objective-C](http://stackoverflow.com/questions/1332389/when-to-call-self-myobject-vs-just-calling-myobject-in-objective-c) and http://stackoverflow.com/questions/536388/iphone-different-between-self-and-normal-variable and http://stackoverflow.com/questions/4088801/ivar-property-access-via-self and http://stackoverflow.com/questions/6360147/syntax-for-accessing-instance-variables-objective-c and [lots of others](http://stackoverflow.com/search?q=%5Bobjc%5D+self.prop+ivar+property&submit=search) – jscs Dec 01 '11 at 21:20

4 Answers4

2

You have to understand that using self.property is a method call (getter or setter method), not a simple assignment.

You should use the ivar directly only inside the setter or getter body.

Reasons:

1/ Properties are meant to shield you from the retain-release hell. Let's imagine you have a "assign" property, you are using the ivar directly and then you decide changing it to "retain". What you get is a bug difficult to find.

2/ Setters and getters can do additional functionality (e.g. setter can add/remove observers to an object) or logging. If you are using the ivar directly you miss this special functionality.

3/ Setters and getters can be overriden in a subclass.

In small projects you can probably avoid most problems even if you are using ivars directly but on big projects, programmed by a team, you should use only self.property to reduce bugs and improve code maintainability. It's also a good idea to give your ivar a different name (like property_ or _property because you'll notice when you are using it without self..

If you are not writing a high performance game or mathematic algorithms, don't worry about worse performance when using self.property.

Sulthan
  • 128,090
  • 22
  • 218
  • 270
1

It depends on the case, if you are just using @synthesize to auto-generate the getters, then it would not cause any problems on the getters. Although common OO practices tell you to use encapsulation, you will notice pretty much all apple sample code accesses the ivar directly.

Another common practice to refer to the ivar without using self is to synthesize like this:

@synthesize myVar=_myVar

and use _myVar when referring to that variable.

It would only cause a problem, if you implemented something in your getter, instead of using @synthesize.

As for the setters, it isn't exactly a problem, but you just have to keep in mind that the properties for that ivar will only be applied if you do self.myIvar as opposed to accessing the iVar directly, so for example a property declared as (retain), will only be retained if you do self.myIvar = newValue as opposed to myIvar = newValue.

Oscar Gomez
  • 18,436
  • 13
  • 85
  • 118
0

The issue arises with properties. if you do not add the self., then you end up assigning to the variable directly, missing out on the property attributes e.g. retain, assign and thus messing up reference counting for the item, and thus causing potential memory leaks.

You should use self, unless you have an explicit reason not to.

Anya Shenanigans
  • 91,618
  • 3
  • 107
  • 122
  • Can someone confirm this? This seems to be the easiest answer to grasp for me. I'm also curious what explicit reasons there would be to not use self. – Hackmodford Dec 01 '11 at 17:10
0

You must use it with clever and clear understanding. If you're using so-called dot-syntax (i.e. self.myVariable) it means you're calling a getter or setter of the property, which is actually a selector sending to an object instance, which is pretty heavy within Objective-C run-time. So, if you need just a value of your var - you can call it once and reuse saved state or call directly to i-var (if permissions allow).

Call for getters/setters when you really need them.

Denis
  • 6,313
  • 1
  • 28
  • 27