需要帮助理解 iOS/Objective-C 编程中的特定分配/释放习惯用法
我是一名经验丰富的 C/C++ 程序员,开始学习 Objective-C 开发。我目前正在查看 UICatalog 示例,并遇到了我在多个地方见过但从未理解过的惯用语的另一个实例。
代码:
ButtonsViewController *buttonsViewController = [[ButtonsViewController alloc] initWithNibName:@"ButtonsViewController" bundle:nil];
[self.menuList addObject:[NSDictionary dictionaryWithObjectsAndKeys:
NSLocalizedString(@"ButtonsTitle", @""), kTitleKey,
NSLocalizedString(@"ButtonsExplain", @""), kExplainKey,
buttonsViewController, kViewControllerKey, nil]];
[buttonsViewController release];
据我所知,这会分配并初始化一个 ButtonsViewController,为 ButtonsViewController 创建一个 NSDictionary 并将该字典添加到一个名为 menuList 的 NSMutableArray (这是上面代码所在的 MainViewController 的成员变量),然后释放它刚刚创建的 ButtonsViewController 。稍后,MainViewController 在必要时使用字典条目将视图切换到buttonsViewController。
我的问题是:为什么在这段代码之后,buttonsViewController 仍然有效?它被分配和释放,其间没有“保留”。向 NSDictionary 或 NSMutableArray 添加某些内容是否具有隐式“保留”?如果是这样,我是否应该能够以某种方式弄清楚这一点,或者这只是你必须阅读和记住的事情之一?
I'm an experienced C/C++ programmer starting to learn Objective-C development. I'm currently looking through the UICatalog sample and came across another instance of an idiom I've seen several places and never understood.
Code:
ButtonsViewController *buttonsViewController = [[ButtonsViewController alloc] initWithNibName:@"ButtonsViewController" bundle:nil];
[self.menuList addObject:[NSDictionary dictionaryWithObjectsAndKeys:
NSLocalizedString(@"ButtonsTitle", @""), kTitleKey,
NSLocalizedString(@"ButtonsExplain", @""), kExplainKey,
buttonsViewController, kViewControllerKey, nil]];
[buttonsViewController release];
AFAIK, this allocates and initializes a ButtonsViewController, creates an NSDictionary for the ButtonsViewController and adds the dictionary to an NSMutableArray called menuList (which is a member variable of the MainViewController where the above code lives) and then releases the buttonsViewController that it just created. Later, the MainViewController uses the dictionary entry to switch views to the buttonsViewController when necessary.
My question is: why is the buttonsViewController still valid after this code? It was allocated and released with no 'retains' between. Does adding something to an NSDictionary or NSMutableArray have an implicit 'retain'? If so, was I supposed to be able to figure that out somehow, or is it just one of those things you have to read and remember?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是 Cocoa 框架中集合保留它们所包含的对象的标准过程。所以是的,可变数组保留了视图控制器。
情况总是如此,除非文档另有说明。
It is standard procedure in the Cocoa frameworks for collections to retain the objects they contain. So yes, the mutable array is retaining the view controller.
This is always the case, unless the documentation says otherwise.
因此,虽然该对象确实是有效的,因为集合保留了它,但我不确定以这种方式思考它是否有用。如果您打算在将对象添加到集合后在该方法中使用该对象,则在使用完毕之前不应释放它。
思考的方式是这样的:
1)我对我创建的这个对象拥有所有权声明
2)现在集合和我都拥有这个对象的所有权
3)现在只有集合拥有,我不应该使用对象,因为我没有它的所有权
,并且忽略了这样一个事实:在这种情况下,从技术上讲,您可以不关注 3 而逃脱惩罚。
这使您可以继续使用 NARC(new-alloc-retain-copy)规则来考虑什么保留对象。
So, while it's true that the object is valid afterwards because the collection retains it, I'm not sure it's useful to think of it that way. If you intend to use the object in that method after adding it to the collection, you shouldn't release it until you're done with it.
The way to think about it is this:
1) I have a claim of ownership of this object I created
2) Now the collection and I both have ownership of this object
3) Now just the collection does, and I shouldn't use the object because I don't have ownership of it
and ignore the fact that you can technically get away with not paying attention to 3 in this case.
This allows you to keep using the NARC (new-alloc-retain-copy) rule for thinking about what retains objects.
如前所述,NSDictionary 和 NSArray 向添加的对象发送保留消息,并在删除时向对象发送释放消息。上面的代码从概念上讲将 ButtonViewController 的所有权转移到字典。您如何从文档中了解这一点的示例:
-addObject:forKey:
-删除所有对象
As mentioned, NSDictionary and NSArray send retain messages to objects added and also send release to objects when removed. The code above is conceptually transferring ownership of buttonsViewController to the dictionary. An example of how you might know this... from the documentation :
-addObject:forKey:
-removeAllObjects
是的,像 NSDictionary 和 NSMutableArray 这样的容器会保留(或者复制,在字典键的情况下)您放入其中的对象。
请参阅 Apple 的集合编程主题指南。
Yes, containers like NSDictionary and NSMutableArray retain (or copy, in the case of dictionary keys) the objects you put into them.
See the Collections Programming Topics guide from Apple.