NSMutableDictionary removeObjectForKey 为未释放的 var 抛出 EXC_BAD_ACCESS

发布于 2024-08-21 20:45:15 字数 1445 浏览 7 评论 0原文

我有一个清单。它有几十个键,每个键都有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

有木有妳兜一样 2024-08-28 20:45:15

这并不是因为 item 没有被释放,而是因为 item 是不可变的。

在 for-in 循环中,无法更改正在遍历的迭代器。如果这样做,程序将抛出变异枚举异常。

您可以

  • 将所有 obj 收集到一个单独的数组中,然后在循环末尾使用 -removeObjectsForKeys:
  • 迭代项目键的副本。具体来说,for (id obj in [item allKeys])

It's not because item is not released, but item 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

  • gather all obj into a separate array, then -removeObjectsForKeys: at the end of the loop; or
  • iterate on a copy of keys of the items. Specifically, for (id obj in [item allKeys]).
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文