无法从CFMutableDictionary中删除值

时间:2011-05-16 06:54:41

标签: objective-c cfmutabledictionary

我试图使用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

2 个答案:

答案 0 :(得分:4)

该值不再在字典中,但仍在内存中,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);

看看会发生什么。

答案 1 :(得分:2)

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