0

I search some documentation, and tried Xcode and AppCode. But I'm still uncertain about few things. So can somebody clarify me:

  1. If I have property named foo, should I have private instance variable named _foo or foo?

  2. If I dont't create a private instance variable, just syntetize it. Then what variable should I try to access self->foo or self->_foo ?

I have seen both approaches and both worked so I'm curious if there is any coding rules or conventions for this because I didn't find any.

Note: I'm not interested in @synthetize foo=_foo;

David Rönnqvist
  • 56,267
  • 18
  • 167
  • 205
The Tosters
  • 417
  • 3
  • 15
  • Both don't work, on the contrary, declaring the *foo ivar* and the *foo property* will make you end up with two confusingly named ivars: `_foo` and `foo`. – Ricardo Sanchez-Saez Jul 22 '13 at 11:08

2 Answers2

2

1. If I have property named foo, should I have private instance variable named _foo or foo?

You don't have to create the instance variable yourself. A instance variable, prefixed with an underscore will automatically be created for you. Since you shouldn't access the variable directly (see below) you shouldn't care about what the underlying variable is called.

2. If I dont't create a private instance variable, just syntetize it. Then what variable should I try to access self->foo or self->_foo ?

Niether. The idea of the property is to use the accessors instead of direct access to the variable. You should use self.foo to get and set the property (or [self foo] and [self setFoo:newFoo]; if you dislike the dot-syntax). Also note that you don't have to explicitly synthesize the property.

David Rönnqvist
  • 56,267
  • 18
  • 167
  • 205
  • 1. Can you provide some link to docs about that? Because I almost certain that both (underscored and not underscored) versions works this same. 2. I'm avare of dot notation, however in some situations I want to access directly to private variable, ignoring accessors. – The Tosters Jul 22 '13 at 09:17
  • [New Features in Xcode 4.4](http://developer.apple.com/library/mac/#documentation/DeveloperTools/Conceptual/WhatsNewXcode/Articles/xcode_4_4.html) – David Rönnqvist Jul 22 '13 at 09:19
  • 1
    [Under what conditions is @synthesize automatic in Objective-c?](http://stackoverflow.com/a/9368743/608157) – David Rönnqvist Jul 22 '13 at 09:20
  • 2
    [Automatic Property Synthesis With Xcode 4.4](http://useyourloaf.com/blog/2012/08/01/property-synthesis-with-xcode-4-dot-4.html) – David Rönnqvist Jul 22 '13 at 09:21
  • You are right, I've moved it there. :-) – Ricardo Sanchez-Saez Jul 22 '13 at 11:08
1

When Xcode auto-synthesizes a property, it automatically adds a supporting instance variable that has the same name as the property with a prepended _. You don't need to manually specify the instance variable, it's automatically there.

Although self->_foo compiles and seems to work, I have never seen anybody using this syntax to access ivars. I find it potentially confusing with the property syntax self.foo, so I would avoid this notation completely. You can access the instance variable by directly typing _foo if you want. But I would also avoid that, unless you are overriding or extending a property accessor method.

I would access the property solely through the property dot notation [1], which IMHO makes code more readable and handles the strong/weak/copy semantics that you specified when declaring the property.

  1. Using property dot notation is syntactic sugar for accessing the property through the auto-syhthesized -(PropertyType)property and -(void)setProperty:(PropertyType)property methods
Ricardo Sanchez-Saez
  • 9,466
  • 8
  • 53
  • 92