属性和合成:在这段代码中,没有“self”?
我有一个简单的问题:
如果我们不在这里使用项目中另一个类中的 dataController 属性,我们实际上不需要使用“@property”并合成,我们可以使用 dataController = controller 执行一个简单的“=”操作,例如在第二块代码中? :
DataController *controller = [[DataController alloc] init];
self.dataController = controller;
[controller release];
rootViewController.dataController = dataController;
第二个:
DataController *controller = [[DataController alloc] init];
dataController = controller;
那么如果我们不需要类外的属性,我们可以这样做吗? 谢谢
i have a simple question :
if we don't use here the dataController property in another class in the project, we don't really need to use the "@property" and synthesize and we could have just done a simple "=" operation with dataController = controller, like in the second chunk of code? :
DataController *controller = [[DataController alloc] init];
self.dataController = controller;
[controller release];
rootViewController.dataController = dataController;
Second one :
DataController *controller = [[DataController alloc] init];
dataController = controller;
So if we don't need a property outside the class, we could just do it this way?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果 dataController 是保留属性,那么您是对的。
如果您只使用一次,那么财产就不会获得太多收益。如果您经常更改其值(即分配不同的 DataController),那么即使是私有属性也有意义,只是为了使内存管理更容易。
You are correct, if dataController is a retaining property.
If you are just using it once, there is not much to gain with a property. If you change its value often (i.e. assign a different DataController) then even a private property can make sense just to make the memory management easier.
如果 dataController 不会在任何其他类中使用,那么您不需要合成它。那么是的,您可以在该类中创建它。
(当然,如果 rootViewController 中的 dataController 也需要对其的引用,那么您也需要处理它)
If dataController is not gonna be used in any other class then you don't need to synthesize it. Then Yes, you could just create it inside that class.
(Of course, if your dataController in your rootViewController also need a reference to it, then you need to handle that as well)
是的,第二个示例工作正常,前提是将 dataController 声明为 iVar 而不仅仅是使用它的方法的局部变量 - 但您可能已经意识到这一点。
在这两个示例中,您当然需要释放 dealloc 方法中存储的值。
Yes, the second example works fine, provided dataController is declared as an iVar and not just a local variable to the method where it is used - but you probably are aware of that.
In both examples you of course need to release the stored value in the dealloc method.