0

I am a silly beginner of iOS xcode. When I am learning objective-C 'self.' is mostly used while searching example codes from others on the internet. But, when we have already synthesized the value it is just the same whether there is 'self.' or not, right?

I wonder when the self is used for a specific situation. Please help me clarify :)

jazzed28
  • 89
  • 2
  • 7

2 Answers2

5

Using self.myProperty is shorthand for [self myProperty]. So it's using the accessor method to retrieve the value.

Accessing the ivar directly (omitting self.) is not the same. For example a subclass could override the myProperty method, changing the returned value. Another common case is lazy initialization: The property getter sets up the value in the first access.

Nikolai Ruhe
  • 81,520
  • 17
  • 180
  • 200
0

There is a difference in property and instance variable (or ivar).

Using self.myVariable you are working with a property, using myVariable you are working with ivar. The rule to remember here is this: "Never work with ivar in your app except of init methods cases". If you are a beginner, just use self everywhere in your code, and you will be OK.

Maksim
  • 2,054
  • 3
  • 17
  • 33