Swissdude
Swissdude

Reputation: 3556

AVFoundation - property declarations - basic question

I'm fiddling around with the AVCamDemo from Apple. Apart from trying to get my head around all that, I ran into something I don't understand.

The properties are initialized in a very strange way and I didn't find any explanation for this.

In the header-file

AVCaptureSession *_session;

...

@property (nonatomic,readonly,retain) AVCaptureSession *session;

in the .m-file

@synthesize session = _session;

What's the story with the underscore???

Thanks for any clarification!

Upvotes: 0

Views: 91

Answers (2)

Codo
Codo

Reputation: 78905

The underscore is a naming convention to distinguish properties from instance variables.

In particular, it helps to distinguish between assignment to the property (that automatically decreases and increases the reference count) and assignment ot the instance variable (without automatic reference count update).

Upvotes: 1

amattn
amattn

Reputation: 10065

Basically, you have two things going on here. an ivar (instance variable) and the property.

  • The ivar is the actual variable.
  • The property is syntactical sugar for getters and setters.

If you do

@synthesize session;

The ivar and the property are assumed to have the same name by the compiler.

if you do

@synthesize session = _session;

Then the property name is session and the ivar name is _session.

MY OPINION: I've been doing Cocoa for over a decade. and @synthesize session = _session; is the safer way. Every now and then, the compiler or the programmer get confused when the ivar and the property have the same name.

Upvotes: 2

Related Questions