从 system_profiler plist 访问项目

Access items from system_profiler plist

我有一个由 system_profiler -xml 命令生成的 plist 文件:

如何获取 cpu_typemachine_name 并将它们存储在 NSString 中?

我尝试了很多方法,甚至将这个文件转换成JSON,但无法获取那些元素。

示例:

strnil

路径好像是这样的:

root \ 0 \ _items \ 0 \ cpu_type root \ 0 \ _items \ 0 \ current_processor_speed ...

这里没有枚举,所以我应该可以直接找到那些键,但我不知道如何。

谢谢!

[arr valueForKey:@"cpu_type"] 将不起作用,因为 arr 是一个数组,而不是字典。要访问您想要的值,试试这个

NSDictionary *rootDictionary = [NSArray arrayWithContentsOfFile:filePath][0];
NSDictionary *itemsDictionary = rootDictionary[@"_items"][0];
NSString *CPUType = itemsDictionary[@"cpu_type"];

注意[0][array objectAtIndex:0]的shorthand,[@"key"][dictionary objectForKey:@"key"]

的缩写

获取 machine_name 将以相同的方式完成

NSString *machineName = itemsDictionary[@"machine_name"];