4

It's now more than 5 months that I'm in Objective-C, I've also got my first app published in the App Store, but I still have a doubt about a core functionality of the language.

When am I supposed to use self accessing iVars and when I'm not?

When releasing an outlet you write self.outlet = nil in viewDidUnload, instead in dealloc you write [outlet release]. Why?

Luca D'Alberti
  • 4,749
  • 3
  • 25
  • 45
Francesco
  • 1,047
  • 11
  • 26

4 Answers4

6

When you write self.outlet = nil the method [self setOutlet:nil]; is called. When you write outlet = nil; you access variable outlet directly.

if you use @synthesize outlet; then method setOutlet: is generated automatically and it releases object before assigning new one if you declared property as @property (retain) NSObject outlet;.

GeneCode
  • 7,545
  • 8
  • 50
  • 85
Nekto
  • 17,837
  • 1
  • 55
  • 65
3

Very very important blog to understand about properties getter-setter method in objective c

                Understanding your (Objective-C) self

http://useyourloaf.com/blog/2011/2/8/understanding-your-objective-c-self.html

PJR
  • 13,052
  • 13
  • 64
  • 104
1

You use self when you are refering to a @property. Usually it will have been @synthesize'd.

You do not use self if you are refering to a "private" variable. Typically, I use properties for UI elements such as UIButtons or for elements I want easily reachable from other classes. You can use the @private, @protected modifiers to explicitly enforce visibility. You cannot however use private methods, that do not exist in Objective-C.

The part about nil, release and dealloc is unrelated to the use of "self". You release what you retained, you nil what is autoretained.

You should read the Objective-C guide, it's well written and very enlightening.

Community
  • 1
  • 1
Kheldar
  • 5,361
  • 3
  • 34
  • 63
  • -1 yes there is such a thing as a private variable, see `@private`. Your reasoning about the use of nil, release and dealloc is incorrect too. `self.foo` is a property. Setting it to nil releases its revious value. `foo` is a variable, setting it to nil causes the old value to leak. The difference is that setting a property to nil is actually sending a message that causes the old value to be released amongst other things. – JeremyP Aug 31 '11 at 10:36
  • @JeremyP You're perfectly right. My memory has failed me :/ My reasoning about the use of nil however is not off (my apps don't leak), and I think my choice of words says exactly what you said yourself. I might be a poor explainer, or you might have jumped to wrong conclusions. Anyway, +1 for refreshing my memory on private modifiers. – Kheldar Aug 31 '11 at 10:45
  • I've removed the down vote because you fixed the bit I down voted for. I still think you have the stuff about use of self confused. – JeremyP Aug 31 '11 at 10:53
0

You use self. when you're accessing properties of class that you're in (hence self). Basically you use self when you want to retain a value, but is only when you have retain in your property definition.

release just releases object that you've retained. You shouldn't release something that you haven't retained cuz it will lead to crash (zombie object).

paxx
  • 1,079
  • 2
  • 12
  • 26