Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios CoreFoundation获取CFDictionaryRef中键的值_Ios_Objective C_Core Foundation - Fatal编程技术网

Ios CoreFoundation获取CFDictionaryRef中键的值

Ios CoreFoundation获取CFDictionaryRef中键的值,ios,objective-c,core-foundation,Ios,Objective C,Core Foundation,我有这个功能 valueForKey(CFDictionaryRef dict, CFStringRef key) { CFTypeRef valueX = CFSTR("key not found"); NSDictionary *settings = (__bridge NSDictionary *)dict; NSString *keyString = (__bridge NSString *)key; NSLog(@"valueForKey: %@ inD

我有这个功能

valueForKey(CFDictionaryRef dict, CFStringRef key) {
    CFTypeRef valueX = CFSTR("key not found");
    NSDictionary *settings = (__bridge NSDictionary *)dict;
    NSString *keyString = (__bridge NSString *)key;
    NSLog(@"valueForKey: %@ inDict: %@ value: %@",keyString,settings,settings[keyString]);
    NSLog(@"allKeys: %@ allValues: %@",settings.allKeys,settings.allValues);

    if (CFDictionaryGetValueIfPresent(dict, key, &valueX)) {
        NSLog(@"CFDictionaryGetValueIfPresent: %@",valueX);
    } else {
        NSLog(@"CFDictionaryGetValueIfPresent not found");
    }
}
valueX打印出“找不到密钥”

它基本上从字典中获取键的值。(我想在一个单独的函数中实现这一点,所以我没有太多的代码重复)

以下是调用函数时的输出:

valueForKey: kEnabled inDict: {
                kEnabled = 1;   
        } value: (null)  allKeys: (
         kEnabled   
        ) allValues: (
                        1   
        )  CFDictionaryGetValueIfPresent not found
因此,键显然在字典中,但是获取键的值返回NULL。真奇怪

所以我做了一个小测试:在我插入的函数的顶部

key = CFSTR("kEnabled");
因此,我硬编码了密钥并重写了参数(在字典中找不到)。这个方法奏效了,我从字典里查到了值

以下是如何创建原始参数字符串:

CFStringRef key = (__bridge CFStringRef)[myObject identifier];
并将其传递给函数


有人知道这有什么问题吗?

我不是专家,但在我看来,CFDictionaryGetValueIfPresent上的文档可能会引起一些思考:“创建字典时提供的密钥哈希和相等回调用于比较。如果散列回调为NULL,则键将被视为指针并转换为整数。如果equal回调为NULL,则使用指针相等(在C中,==)。如果equal回调不理解键或ICT中的任何键,则行为未定义。“因此,默认情况下,NSString
isEqual:
未被使用;字符串必须是对象相同的。谢谢!将&kCFTypeDictionaryKeyCallBacks添加到我的字典创建中修复了它!棒极了。做得好。