Why do you use an underscore for an instance variable, but not its corresponding property? [duplicate]

Reminder

The @property directive is equivalent to declaring both a setter and a getter. In the case of level,

@property CGFloat level;

can be replaced by

- (CGFloat)level;
- (void)setLevel:(CGFloat)v;

Your question

Why declare a property named level for a variable named _level and why name a variable with a leading _ in the first place? I don’t know.

How it works, is answered in LevelMeter.m:

- (CGFloat)level { return _level; }
- (void)setLevel:(CGFloat)v { _level = v; }

Leave a Comment