Underscore prefix on property name? [duplicate]

The use of an underscore for ivar names is a convention first used by Apple to differentiate between an actual ivar and a property. Many people have since adopted this convention.

The reason this is done is to prevent the mistake of assigning a new value to an ivar instead of to the actual setter:

myIvar = newValue;

instead of

self.myIvar = myValue;

If you accidentally use the top example, you could cause a memory leak. The underscore prevents you from making that mistake.

Leave a Comment