何时释放 NSDictionary 实例及其所有值?
我有一本通过以下方式创建和初始化的字典
NSDictionary *dict = [[NSMutableDictionary alloc] initWithCapacity:N];
for (int index = 0; index < N; index++) {
MyObject *obj = [[MyObject alloc] init];
...
[dict setObject:obj forKey:index];
}
所以问题是 1. MyObject 实例是否应该创建为自动释放? 2.一旦我需要释放 NSDictionary 实例,我是否也应该释放所有 MyObject 实例(如果它们不是作为自动释放创建的)
I have a dictionary that's created and initialized in following ways
NSDictionary *dict = [[NSMutableDictionary alloc] initWithCapacity:N];
for (int index = 0; index < N; index++) {
MyObject *obj = [[MyObject alloc] init];
...
[dict setObject:obj forKey:index];
}
So the questions are
1. should the MyObject instances be created as autorelease?
2. once I need to release the NSDictionary instance, should I also release all the MyObject instances (if they are not created as autorelease)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在调用 setObject 后显式释放 MyObject 实例,因为此时它们将被保留。您可以自动释放 MyObject 实例,但显式释放会更有效。释放 NSDictionary 实例将释放 MyObject 实例。
You can explicitly release MyObject instances after calling setObject since they will be retained at that point. You can autorelease MyObject instances, but explicit releasing will be more efficient. Releasing the NSDictionary instance will release MyObject instances.