> On May 20, 2015, at 1:04 PM, Alex Zavatone <z...@mac.com> wrote:
> 
> Many times in the classes, an ivar is defined in the @interface of the class. 
>  Sometimes not.  

In the old days, before about 2006, the ivars had to be defined in the 
@interface. Nowadays it’s best to put them in the @implementation since they’re 
private to the implementation anyway. I always move them when updating old code.

> Now, I remember back in 2012 that I could access a property within an 
> instance of the object without using self, but never knew if I was accessing 
> the ivar or the property itself simply by looking at the object.

It’s simple, really:

• “foo” is a variable — either an ivar or some sort of C variable (local, 
parameter, static, global.)
• “self.foo” is a reference to a property. It’s equivalent to “[self foo]”, or 
if it’s on the left-hand-side of an assignment it’s “[self setFoo: …]”.

There’s no ambiguity at all. A property name _has to_ go after a “.”, with an 
object value beforehand. An instance variable _cannot_ go after a “.” (It’s 
possible to refer to an ivar as “self->foo” but that syntax is old-fashioned 
and discouraged AFAIK.)

Ivars and properties have entirely different namespaces, so even if a property 
has a backing ivar (i.e. is synthesized) the names don’t have to relate to each 
other. If you don’t explicitly use @synthesize, then I believe the ivar names 
created by the compiler will be the property name prefixed with “_”.

I’m strongly against auto-synthesis; I think it was a bad idea to add it to 
Clang. It may save you from having to type “@synthesize”, but it leads to a lot 
of confusion, as shown here, by blurring the distinction between properties and 
ivars and creating ivars out of nowhere. And it can cause bugs if you meant a 
property to have explicit get or set methods but forgot to implement them (or 
worse, misspelled them!) I always turn it off in the target settings of my 
projects.

—Jens
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to