NSMutableDictionary removeObjectForKey 为未释放的 var 抛出 EXC_BAD_ACCESS
我有一个清单。它有几十个键,每个键都有 NSMutableArray 项:
...
<key>KeyName1</key>
<array>
<string>String1</string>
<string>String2</string>
<string>String3</string>
</array>
<key>KeyName2</key>
<array>
<string>String1</string>
<string>String2</string>
<string>String3</string>
</array>
...
我需要删除所有键的“String1”。这是我编写的函数:
void DeleteString(NSString *StringToDelete){
NSMutableDictionary *item=nil;
item = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
for (id obj in item){
NSString *removeThisObject=(NSString *)obj;
NSArray *ObjectArray = [item objectForKey:obj];
for (int i = 0; i< [ObjectArray count]; i++){
NSString *objectString = [ObjectArray objectAtIndex:i];
if([objectString isEqualToString:StringToDelete]){
NSLog(@"Remove This from Item Dictionary: %@",removeThisObject);
[item removeObjectForKey:obj];
}
}
}
[item writeToFile:filePath atomically: YES];
[item release];
}
我认为如果它包含我不想要的值,则删除整个键会更容易,但是行: [item removeObjForKey:obj] 会抛出 EXC_BAD_ACCESS。
这对我来说没有意义,因为据我了解,EXC_BAD_ACCESS 意味着消息已发送到已发布的方法。
在这里,“项目”没有被释放,是吗?我在释放“item”时没有错误,只是低了几行。
这是怎么回事?有没有更好的方法从两个键中删除“String1”?
谢谢。
I have a plist. It has several dozen keys, each with NSMutableArray items:
...
<key>KeyName1</key>
<array>
<string>String1</string>
<string>String2</string>
<string>String3</string>
</array>
<key>KeyName2</key>
<array>
<string>String1</string>
<string>String2</string>
<string>String3</string>
</array>
...
I have need to remove "String1" for all keys. This is the function that I wrote:
void DeleteString(NSString *StringToDelete){
NSMutableDictionary *item=nil;
item = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
for (id obj in item){
NSString *removeThisObject=(NSString *)obj;
NSArray *ObjectArray = [item objectForKey:obj];
for (int i = 0; i< [ObjectArray count]; i++){
NSString *objectString = [ObjectArray objectAtIndex:i];
if([objectString isEqualToString:StringToDelete]){
NSLog(@"Remove This from Item Dictionary: %@",removeThisObject);
[item removeObjectForKey:obj];
}
}
}
[item writeToFile:filePath atomically: YES];
[item release];
}
I figured it'd be easier to remove just the entire key if it includes a value that I do not want, but the line: [item removeObjForKey:obj] throws a EXC_BAD_ACCESS.
This does not make sense to me because, as I understand it EXC_BAD_ACCESS means that a message was sent to a released method.
Here, 'item' isn't released is it? I have no error releasing 'item' just a few lines lower.
What is going on here? Is there a better way to remove "String1" from both Keys?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这并不是因为
item
没有被释放,而是因为item
是不可变的。在 for-in 循环中,无法更改正在遍历的迭代器。如果这样做,程序将抛出变异枚举异常。
您可以
obj
收集到一个单独的数组中,然后在循环末尾使用-removeObjectsForKeys:
; 或for (id obj in [item allKeys])
。It's not because
item
is not released, butitem
is immutable.Within a for-in loop, the iterator that's being traversed cannot be changed. If you do, the program will throw a mutated enumeration exception.
You could
obj
into a separate array, then-removeObjectsForKeys:
at the end of the loop; orfor (id obj in [item allKeys])
.