开发者

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 h开发者_C百科eader-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!


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.


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).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜