“释放>nil>init”更新 NSMutableDictionary 的可接受方法?
下面的方法是更新 NSMutableDictionary 的可接受的(即最佳实践,非 hacky)方法吗?
基本上我想要该方法来检查字典是否已初始化/填充。如果没有,那么我希望它继续并用我拥有的一组 iVar 和匹配的键组填充它。
如果它已经被初始化/填充,我想要刷新,并且我想避免必须为字典中的每个元素写出 setObject:forKey:
。
那么...
- “Release”>“Set nil”>“Re-init”是刷新此字典的可接受方法吗?
- 如果是这样,这是对大值字典执行此操作的唯一方法,除非我想逐行重复
setObject:forKey:
谢谢!代码如下:
- (void) populateFetchDataDict {
if (!self.fetchDataDict) {
self.fetchDataDict = [[NSMutableDictionary alloc] initWithObjectsAndKeys:object1, @"key1", object2, @"key2", object3, @"key3", object4, @"key4", nil];
} else {
[fetchDataDict release];
fetchDataDict = nil;
self.fetchDataDict = [[NSMutableDictionary alloc] initWithObjectsAndKeys:object1, @"key1", object2, @"key2", object3, @"key3", object4, @"key4", nil];
}
}
小注释:
fetchDataDict
是一个 iVar,所有对象也是如此,因此假设object1
、object2
等实际上会beself.object1
,self.object2
...- 另外,我直接访问了
fetchDataDict
iVar 来进行release
并nil
,就像我一样我认为这就是我们应该做的,但如果我错了,请纠正我。
本题为学习题,欢迎长篇大论!
Is the method below an acceptable (i.e. best-practices, non-hacky) way of updating an NSMutableDictionary?
Basically I want the method to check if the dictionary has been initialized/populated yet. If it has NOT then I want it to go ahead and populate it with a certain set of iVars I have, with the matching set of keys.
If it HAS already been initialized/populated though, I want a refresh, and I wanted to avoid having to write out setObject:forKey:
for every single element in the dict.
So...
- Is "Release">"Set nil">"Re-init" an acceptable approach to refreshing this dictionary?
- If so, is that the only way to do it for a dictionary of large values unless I want to repeat line after line of
setObject:forKey:
Thanks! Code follows:
- (void) populateFetchDataDict {
if (!self.fetchDataDict) {
self.fetchDataDict = [[NSMutableDictionary alloc] initWithObjectsAndKeys:object1, @"key1", object2, @"key2", object3, @"key3", object4, @"key4", nil];
} else {
[fetchDataDict release];
fetchDataDict = nil;
self.fetchDataDict = [[NSMutableDictionary alloc] initWithObjectsAndKeys:object1, @"key1", object2, @"key2", object3, @"key3", object4, @"key4", nil];
}
}
Minor notes:
fetchDataDict
is an iVar, as are all of the objects, so assume theobject1
,object2
, etc. would actually beself.object1
,self.object2
...- Also, I accessed the
fetchDataDict
iVar directly forrelease
andnil
, as I thought that's what we're supposed to do, but please correct me if I'm wrong.
This question is learning-focused, so long explanations are welcome!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于您正在使用属性访问器,并且我假设您将属性标记为
retain
,因此您可以简单地设置新值 - 旧值将自动释放。事实上,您的代码现在没有崩溃的唯一原因是因为您没有自动释放它们而过度保留了新字典,而您应该这样做。事实上,您的代码可以简单地如下所示:
如果您只想在现有字典中设置这些特定值,那么您可以在 if 语句中添加回来并将这些默认值与现有字典合并:
Since you are using property accessors, and I am assuming you have the properties marked as
retain
, you can simply set the new value - the old will be released automatically. In fact the only reason your code doesn't crash right now is because you are over-retaining the new dictionaries by not autoreleasing them, which you should do.In fact your code can simply look like:
If you only wanted to set those specific values in an existing dictionary, then you could add back in the if statement and merge in those default values with the existing dictionary: