我在一些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'的东西不是结构或联合
在Xcode 4中创建新项目时,样板代码在将实现文件中的ivars合成为时,会添加下划线字符:
@synthesize window = _window;
Run Code Online (Sandbox Code Playgroud)
要么:
@synthesize managedObjectContext = __managedObjectContext;
Run Code Online (Sandbox Code Playgroud)
有人能告诉我这里完成了什么吗?我不是一个完整的润滑剂,但这是客观的一个方面 - 我不明白.
另一个困惑点; 在app委托实现中,在如上所述合成窗口iVar之后,在应用程序didFinishLaunchingWithOptions:方法中,使用self引用窗口和viewController ivars:
self.window.rootViewController = self.viewController
[self.window makeKeyAndVisible];
Run Code Online (Sandbox Code Playgroud)
但是在dealloc方法中它是_window或_viewController
谢谢
我以前在变量名称中避免使用下划线,这可能是我大学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)
那为什么强调那里?它有特殊意义吗?我必须知道的一项公约?
我注意到在那里的很多参考资料中,我看到很多时候,变量在.h文件中被命名为_variable,然后在.m文件中被@ synthesize.
@synthesize variable = _variable;
Run Code Online (Sandbox Code Playgroud)
为什么这样做?我错过了什么?
谢谢!
可能重复:
属性名称上的下划线前缀?
这是什么意思?@synthesize window=_window;我知道一般来说这意味着'某个类'有一个窗口,但为什么要使用_window而不仅仅是window?这是名称空间吗?
在目标CI中,在变量名称之前看到许多带有下划线的代码,例如_someVariable
这是为什么?还有如何编写访问器,即获取和设置这种变量的方法.
任何人都可以向我指出使用下划线的解释,我一直认为它们用于突出显示您正在访问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) 我不完全清楚(除了代码的可读性之外),为什么要在创建属性时创建带下划线前缀的内部变量.
由于所有内容都是在内部处理的,为什么还要这样做,因为我们不向getter和setter添加任何代码?
即使我必须向getter或setter添加一些代码,我也不明白为什么我不能只检查myvar而不是检查_myvar然后将其分配给myvar.
任何人都可以给我一些解释,除了"这样做是因为这是每个人都做的事情吗?" 我想了解这种做法背后的全部原因(即使没有getter和setter的自定义代码,这似乎也很常见).
谢谢!
第一个图像使用self.name来改变,第二个图像使用_name来改变.它应该是相同的结果,但是第二个图像输出没有.为什么?


这是代码
#import "ViewController.h"
@interface kvo : NSObject
@property (nonatomic,strong) NSString *name;
@end
@implementation kvo
- (void)change
{
_name = @"b";
}
@end
@interface ViewController ()
@property (nonatomic, strong) kvo *a1;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.a1 = [[kvo alloc] init];
_a1.name = @"a";
[self.a1 addObserver:self forKeyPath:@"name" options:NSKeyValueObservingOptionNew context:nil];
[_a1 change];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
NSLog(@"1");
}
Run Code Online (Sandbox Code Playgroud)
所不同的是self.name和_name在变化的方法
编辑:这与"Objective-C中_variable和self.variable之间有什么区别?[复制]"的问题不一样,我知道这是关于getter方法和setter方法的,我的问题是为什么setter方法会触发KVO并且_name = @"b"不会解雇KVO.
我是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是什么用.请解释一下.谢谢.
objective-c ×11
cocoa-touch ×5
iphone ×4
cocoa ×3
coding-style ×2
ios ×2
ios4 ×1
properties ×1
swift ×1
syntax ×1
variables ×1