【问题标题】:Could not remove value from a CFMutableDictionary无法从 CFMutableDictionary 中删除值
【发布时间】:2011-05-16 06:54:40
【问题描述】:

我试图使用 CFDictionaryRemoveValue 从字典中删除键值对。 但它不会删除键和值。它也会在删除后打印键值对。

struct session *value = CFDictionaryGetValue(cfmdict,tiId); 
NSLog(@"The value is %d and %c", value->a, value->c); 
CFDictionaryRemoveValue(cfmdict,tiId); 
NSLog(@"The value is %d and %c", value->a, value->c);

输出

The value is 12 and L
The value is 12 and L

【问题讨论】:

    标签: objective-c cfmutabledictionary


    【解决方案1】:

    该值不再在字典中,但仍在内存中,value 仍然指向那里。试试:

    struct session *value = (struct session *)CFDictionaryGetValue(cfmdict,tiId); 
    NSLog(@"The value is %d and %c", value->a, value->c); 
    CFDictionaryRemoveValue(cfmdict,tiId); 
    value = (struct session *)CFDictionaryGetValue(cfmdict,tiId); 
    NSLog(@"The value is %d and %c", value->a, value->c);
    

    看看会发生什么。

    【讨论】:

    • @MByD: 收到警告:赋值从指针目标类型中丢弃限定符并获得运行时异常为 EXC_BAD_ACCESS
    • 你从哪里得到警告?关于 EXC_BAD_ACCESS -> 这就是我想让你看到的。现在没有返回值:)
    • 我在 NSLog(@"The value is %d and %c",value->a,value->c); stmt 最后。我插入了两个值,我正在删除一个。
    • @MByD:它现在可以工作了。谢谢。是否可以获得所有值。
    • @ron - 是的,请参阅文档以了解适合您的需求 --> developer.apple.com/library/ios/#documentation/CoreFoundation/…
    【解决方案2】:

    您对CFDictionaryGetValue 的第一次调用会返回一个指向某个结构的指针。然后从字典中删除指向该结构的指针,但这不会影响已存储在 value 变量中的值。

    【讨论】:

      猜你喜欢
      • 2018-12-07
      • 2018-12-30
      • 2019-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-04
      • 2020-12-15
      相关资源
      最近更新 更多