Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.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
Objective c 提取CFKeyedArchiverUID值_Objective C_Cocoa_Plist - Fatal编程技术网

Objective c 提取CFKeyedArchiverUID值

Objective c 提取CFKeyedArchiverUID值,objective-c,cocoa,plist,Objective C,Cocoa,Plist,我有一个plist文件,其中包含一些“对象”。我可以成功提取字典,但当我尝试NSLog一个objectForKey时,我得到了这种日志: <CFKeyedArchiverUID 0x608000228c20 [0x7fff75bddf00]>{value = 815} {value=815} 如何提取值815 也许知道815是对字典中某个项目位置的引用会有所帮助 谢谢因为我找不到解决方案,所以我使用了一个技巧,将对象转换为字符串,并使用正则表达式提取括号{}之间的部分。下面是代码

我有一个plist文件,其中包含一些“对象”。我可以成功提取字典,但当我尝试NSLog一个objectForKey时,我得到了这种日志:

<CFKeyedArchiverUID 0x608000228c20 [0x7fff75bddf00]>{value = 815}
{value=815}
如何提取值815

也许知道815是对字典中某个项目位置的引用会有所帮助


谢谢

因为我找不到解决方案,所以我使用了一个技巧,将对象转换为字符串,并使用正则表达式提取括号{}之间的部分。下面是代码,其中value是NSDictionary中的一个对象:

NSError *error = nil;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"\\{(.*?)\\}" options:NSRegularExpressionCaseInsensitive error:&error];
NSString *result = [[[value objectForKey:@"artist"] description] substringWithRange:[regex rangeOfFirstMatchInString:[[value objectForKey:@"artist"] description] options:0 range:NSMakeRange(0, [[value objectForKey:@"artist"] description].length)]];

由于找不到解决方案,我使用了一个技巧,将对象转换为字符串,并使用regex提取括号{}之间的部分。下面是代码,其中value是NSDictionary中的一个对象:

NSError *error = nil;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"\\{(.*?)\\}" options:NSRegularExpressionCaseInsensitive error:&error];
NSString *result = [[[value objectForKey:@"artist"] description] substringWithRange:[regex rangeOfFirstMatchInString:[[value objectForKey:@"artist"] description] options:0 range:NSMakeRange(0, [[value objectForKey:@"artist"] description].length)]];
据此,CFKeyedArchiverUID是一个结构:

struct __CFKeyedArchiverUID {
  CFRuntimeBase _base;
  uint32_t _value;
};
CFRuntimeBase是16位。()

因此,您可以通过跳过前16位并获取该地址处的uint32_t值来获取CFKeyedArchiverUID对象的值:

+ (uint32_t)valueForKeyedArchiverUID: (id)keyedArchiverUID {
    void *uid = (__bridge void*)keyedArchiverUID;
    uint32_t *valuePtr = uid+16;
    return *valuePtr;
}
据此,CFKeyedArchiverUID是一个结构:

struct __CFKeyedArchiverUID {
  CFRuntimeBase _base;
  uint32_t _value;
};
CFRuntimeBase是16位。()

因此,您可以通过跳过前16位并获取该地址处的uint32_t值来获取CFKeyedArchiverUID对象的值:

+ (uint32_t)valueForKeyedArchiverUID: (id)keyedArchiverUID {
    void *uid = (__bridge void*)keyedArchiverUID;
    uint32_t *valuePtr = uid+16;
    return *valuePtr;
}

是的,我发现@Andrew的答案最适合你的工作,但如果你想知道如何创建一个CFKeyedArchiverUID,我终于想出了方法

  • 在项目中包括CoreFoundation并将其导入文件:

    #import <CoreFoundation/CoreFoundation.h>
    
  • 在要创建KeyedArchiverUID的方法中,只需使用以下命令:

    uint32_t value = ...; // Value here
    
    id uid = _CFKeyedArchiverUIDCreate(CFAllocatorGetDefault(), value);
    

  • 希望这能帮助任何想创建KeyedArchiverUID的人是的,我发现@Andrew的答案对你的工作来说是最好的,但万一你想知道如何创建一个CFKeyedArchiverUID,我终于想出了方法

  • 在项目中包括CoreFoundation并将其导入文件:

    #import <CoreFoundation/CoreFoundation.h>
    
  • 在要创建KeyedArchiverUID的方法中,只需使用以下命令:

    uint32_t value = ...; // Value here
    
    id uid = _CFKeyedArchiverUIDCreate(CFAllocatorGetDefault(), value);
    

  • 希望这能帮助那些正在研究如何创建KeyedArchiverUID的人。解决这个问题的合适方法是按照建议做类似的事情:

  • 在源代码中声明以下内容:

    //我们知道CoreFoundation中存在这种情况
    uint32_t_CFKeyedArchiverUIDGetValue(id uid);
    
  • 然后像这样使用它(即):

    NSUInteger objDictIdx=
    _CFKeyedArchiverUIDGetValue(fileData[@“$top”][@“root”]);
    

  • 解决这一问题的正确方法是按照建议做类似的事情:

  • 在源代码中声明以下内容:

    //我们知道CoreFoundation中存在这种情况
    uint32_t_CFKeyedArchiverUIDGetValue(id uid);
    
  • 然后像这样使用它(即):

    NSUInteger objDictIdx=
    _CFKeyedArchiverUIDGetValue(fileData[@“$top”][@“root”]);
    

  • 您好,谢谢,这很有效,但是您是否也知道如何创建一个带有自定义值的CFKeyedArchiverUID并将其添加到NSDictionary中?请注意,CFRuntimeBase实际上不是16位;它是指针的大小加上32位。请参见中的
    struct\uu CFRuntimeBase
    。否则答案很好,谢谢!您好,谢谢,这很有效,但是您是否也知道如何创建一个带有自定义值的CFKeyedArchiverUID并将其添加到NSDictionary中?请注意,CFRuntimeBase实际上不是16位;它是指针的大小加上32位。请参见中的
    struct\uu CFRuntimeBase
    。否则答案很好,谢谢!这是一个很好的答案。也许你可以问一个关于创建
    CFKeyedArchiverUID
    s的新问题,并将其作为答案发布在那里,以提高可见性?这是一个很好的答案。也许您可以问一个关于创建
    CFKeyedArchiverUID
    s的新问题,并将其作为答案发布在那里,以提高可见性?