我在一些iPhone示例中看到,属性在变量前面使用了下划线_.有谁知道这意味着什么?或者它是如何工作的?
我正在使用的接口文件如下所示:
@interface MissionCell : UITableViewCell {
Mission *_mission;
UILabel *_missionName;
}
@property (nonatomic, retain) UILabel *missionName;
- (Mission *)mission;
Run Code Online (Sandbox Code Playgroud)
我不确定上面做了什么,但是当我尝试设置任务名称时:
aMission.missionName = missionName;
Run Code Online (Sandbox Code Playgroud)
我收到错误:
请求成员'missionName'的东西不是结构或联合
我以前在变量名称中避免使用下划线,这可能是我大学Java时代的延续.因此,当我在Objective C中定义一个属性时,这就是我自然而然的事情.
// In the header
@interface Whatever
{
NSString *myStringProperty
}
@property (nonatomic, copy) NSString *myStringProperty;
// In the implementation
@synthesize myStringProperty;
Run Code Online (Sandbox Code Playgroud)
但在几乎所有的例子中都是如此
// In the header
@interface Whatever
{
NSString *_myStringProperty
}
@property (nonatomic, copy) NSString *myStringProperty;
// In the implementation
@synthesize myStringProperty = _myStringProperty;
Run Code Online (Sandbox Code Playgroud)
我应该克服对下划线的厌恶,因为这是应该做的一种方式,这种风格是否是一个很好的理由?
更新:现在使用自动属性合成你可以省略@synthesize,结果和你使用的一样
@synthesize myStringProperty = _myStringProperty;
Run Code Online (Sandbox Code Playgroud)
这清楚地表明了Apple的偏好.我已经学会了停止担忧并且喜欢下划线.
我在Apple,UIPickerView.h中看过这个:
id<UIPickerViewDataSource> _dataSource;
Run Code Online (Sandbox Code Playgroud)
那为什么强调那里?它有特殊意义吗?我必须知道的一项公约?
任何人都可以向我指出使用下划线的解释,我一直认为它们用于突出显示您正在访问iVar [_window release];
而不是通过setter/getter方法访问iVar,[[self window] release];
或者[self.window release];
我只是想验证我的理解是正确.
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UILabel *markerLabel;
@synthesize window = _window;
@synthesize markerLabel = _markerLabel;
Run Code Online (Sandbox Code Playgroud) 注意:对于那些试图理解这一点的人来说,我想出了我的困惑的根源.在.h,我有:
...
@interface myClass : parentClass {
className *variableName:
}
@property (strong, nonatomic) className *variableName;
...
Run Code Online (Sandbox Code Playgroud)
这导致self.variableName和_variableName是.m中的两个不同变量.我需要的是:
...
@interface myClass : parentClass {
className *_variableName:
}
@property (strong, nonatomic) className *variableName;
...
Run Code Online (Sandbox Code Playgroud)
然后,在类'.m中,self.variableName和_variableName是等价的
在全新的Xcode的4.5+,与ARC,针对iOS 5.0及项目,有一个明显的优势(运行时的效率,速度等)使用_variableName
了self.variableName
与旧式@synthesize variableName
?
我的理解是Xcode 4.5+将创建一个_variableName
相当于的默认访问器,self.variableName
并且唯一不使用的原因@synthesize variableName
是为了避免iVars和传入变量之间的混淆,对吗?
对我来说,只是使用self.variableName
访问iVar似乎是最直接和明确的,你正在寻找哪个变量.除了打字_
与self.
,使用是否有优势_variableName
?
我是iphone开发的新手.我正在研究iphone中的录音.我已经从Apple下载了"speak here"示例程序.它包含LevelMeter.h文件,其中
@interface LevelMeter : UIView {
CGFloat _level, _peakLevel;
}
Run Code Online (Sandbox Code Playgroud)
该物业被设定为
@property CGFloat level;
@property CGFloat peakLevel;
Run Code Online (Sandbox Code Playgroud)
声明像_level这样的变量并将其属性设置为level是什么用.请解释一下.谢谢.
所以我发现在合成属性时你必须使用下划线,这对我来说没有任何意义.
那么,让我们开始吧.在我们的.h文件中,我们写了这一行:
@property (nonatomic) double speed;
Run Code Online (Sandbox Code Playgroud)
在我们的.m文件中,我们这样做:
@synthesize speed = _speed;
Run Code Online (Sandbox Code Playgroud)
为什么?据我所知,property生成一个实例变量并为它创建setter和getter.但到底是怎么回事
@synthesize speed = _speed
Run Code Online (Sandbox Code Playgroud)
做?常识告诉我,我们将_speed中的值赋予速度.好的.我们在哪里宣布_speed?为什么编译器没有给我们错误?它应该是什么意思?为什么这样混淆代码?
我的问题在这里:
如果我这样做会发生什么
@synthesize speed;
Run Code Online (Sandbox Code Playgroud)
没有_speed,我会得到错误或一些错误吗?这种语法后的原因是什么?制作时他们在想什么?_speed来自哪里?它是什么?它是指针还是真正的价值?到底是怎么回事?
objective-c ×7
cocoa-touch ×4
cocoa ×3
iphone ×3
coding-style ×1
ios ×1
syntax ×1
synthesize ×1
variables ×1