Xcode 混乱 - 属性和合成、保留?
我看到属性和合成在没有先“声明变量”的情况下被使用......并且我对使用哪些属性有点困惑。我想在 AppDelegate 中分配并初始化我的 viewController,然后确保它在剩余的运行中存在。显然我想要保留财产?.. 然而..由于 alloc 返回的 viewController 的保留计数为 1,因此仅使用保留属性似乎更聪明。没有其他班级会使用我的 setter,所以我不在乎?
前任。
在AppDelegate.h中:
@propert(nonatomic,retain) MyViewController *myViewController;
在AppDelegate.m中:
@synthesize myViewController = _myViewController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.myViewController = [[[EventDataController alloc] init] autorelease];
[self.window makeKeyAndVisible];
return YES;
}
或..
在AppDelegate.h中:
@propert(nonatomic) MyViewController *myViewController;
在AppDelegate.m中: >
@synthesize myViewController = _myViewController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.myViewController = [[EventDataController alloc] init];
[self.window makeKeyAndVisible];
return YES;
}
请让我明白。
I see the property and synthesize being used without "declaring the variable" first.. and I'm a bit confused about what properties to use. I want to alloc and init my viewController in AppDelegate and then be sure it's there for the remainder of the run. Clearly I want retain-property?..
however.. since alloc returns the viewController with retain count 1, it seams a lot smarter to just use leave the retain-property out. No other class will use my setter, so I don't care then?
Ex.
in AppDelegate.h:
@propert(nonatomic,retain) MyViewController *myViewController;
in AppDelegate.m:
@synthesize myViewController = _myViewController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.myViewController = [[[EventDataController alloc] init] autorelease];
[self.window makeKeyAndVisible];
return YES;
}
or..
in AppDelegate.h:
@propert(nonatomic) MyViewController *myViewController;
in AppDelegate.m:
@synthesize myViewController = _myViewController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.myViewController = [[EventDataController alloc] init];
[self.window makeKeyAndVisible];
return YES;
}
Set me straight, please.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不再需要为属性定义实例变量 -
@synthesize
会为您做到这一点。至于你的其他问题 - 你可以用任何一种方式来做,但不要忘记在你的
-dealloc
中释放它。我喜欢(nonatomic,retain)
,因为它非常清晰且易于使用/理解。您所做的就是分配,它会完成其余所有操作:在您的
-dealloc
中:在手动释放的情况下,您可能想完全忘记属性,只使用如下所示的实例变量:
在您的实现中:
在您的
-dealloc
中:请注意,
nil
的最后一个分配可能是不必要的,但我遇到了太多难以跟踪的错误(这就是为什么我喜欢保留的属性 - 您所需要做的就是设置他们为零)。我尝试始终使用保留的属性,因为这可以节省我的一些大脑周期,而且我不关心可以为 CPU 节省的纳秒。
You don't need to define instance variables for properties anymore -
@synthesize
does that for you.As for your other question - you can do it either way, but don't forget to release it in your
-dealloc
. I like(nonatomic, retain)
, because it is very clear and easy to use/understand. All you do is assign and it does all the rest:In your
-dealloc
:In the case of manually releasing you might want to forget about properties at all and only use instance variables like this:
In your implementation:
In your
-dealloc
:Note that the last assignment of
nil
might be unnecessary, but I've had too many hard-to-track bugs with this (and that's why I love retained properties - all you need to do is set them to nil).I try to always go with retained properties as this saves me some brain cycles and I don't care about the nanosecond I could save for the CPU otherwise.
我会选择第一个选项。
为什么在完成后不能显式释放
myViewController
?我不会做出“这个类永远不会被其他人使用”之类的假设。
I would go with the first option.
Why can't you explicitly release
myViewController
when you're done with it?I wouldn't make assumptions like "this class will never be used by anybody else".
在选项中,我更喜欢使用
retain
声明属性。这是为您准备的文档,它可以帮助您避免由于静态分析等工具而犯的愚蠢错误。由于您的类拥有控制器,因此应该声明它拥有对它的引用,而不管实现细节如何。偏离传统习惯用法是引入错误的好方法。您还应该将其声明为只读。of the options, i prefer declaring the property with
retain
. this is documentation for your sake, and it can save you from silly mistakes because of tools such as static analysis. since your class owns the controller, it should be declared that it holds a reference to it, regardless of implementation details. deviation from conventional idioms is a good way to introduce bugs. you should also declare it asreadonly
.