在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的偏好.我已经学会了停止担忧并且喜欢下划线.
如何在H2中使用名为GROUP的列创建表?我看到一个例子,前一段时间使用类似[*]的东西,但我似乎无法找到它.
在我的上一个问题(这里),我有一个问题,我得到一个EXC_BAD_ACCESS,因为我发布了我刚刚分配的变量:
NSMutableArray* s = [[NSMutableArray alloc] init];
stack = s;
[s release];
Run Code Online (Sandbox Code Playgroud)
本来应该
NSMutableArray* s = [[NSMutableArray alloc] init];
stack = s;
Run Code Online (Sandbox Code Playgroud)
但是,stack是我的类的保留属性.它的声明如下:
@interface StateStack ()
@property (nonatomic, retain) NSMutableArray* stack;
@end
Run Code Online (Sandbox Code Playgroud)
我的印象是,当您指定'retain'变量时,它会自动增加对象的retainCount.所以你应该从释放指针开始(如此处所示).
为什么这两种情况不同?谢谢!