Obj-C @synthesize

use*_*359 4 iphone xcode objective-c

可能重复:
在Objective C中使用下划线前缀属性名称

iPhone App Developer初学者:

在.h

@property (nonatomic, retain) IBOutlet UILabel *detailDescriptionLabel;
Run Code Online (Sandbox Code Playgroud)

在.m

@synthesize detailDescriptionLabel = _detailDescriptionLabel;
Run Code Online (Sandbox Code Playgroud)

我以前常常看到

@synthesize detailDescriptionLabel;
Run Code Online (Sandbox Code Playgroud)

= _让我失望了,这是做什么的?

Dee*_*olu 6

每个属性都由实例变量支持.该语言允许以不同的方式命名它们.通过这样做@synthesize detailDescriptionLabel = _detailDescriptionLabel;,你基本上是说使用它_detailDescriptionLabel作为属性的后备实例变量detailDescriptionLabel.如果你这样做@synthesize detailDescriptionLabel;,它会隐含地理解实例变量具有相同的名称.

  • 请注意,用户代码不应使用前面的下划线字符:http://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CodingGuidelines/Articles/NamingBasics.html#//apple_ref/doc/uid/20001281- 1002931-BBCFHEAB (2认同)
  • 确实如此.但是,你看看XCode 4的界面构建器生成的代码了吗?它确实在大多数时候添加下划线. (2认同)