【问题标题】:Access items from system_profiler plist从 system_profiler plist 访问项目
【发布时间】:2015-04-01 05:07:51
【问题描述】:

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

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

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

例子:

strnil

路径似乎是这样的:

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

这里没有枚举,所以我应该可以直接得到那些键,但我不知道怎么做。

谢谢!

【问题讨论】:

    标签: objective-c macos plist


    【解决方案1】:

    [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]的简写,[@"key"][dictionary objectForKey:@"key"]的简写

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

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

    【讨论】:

      猜你喜欢
      • 2021-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-16
      • 2017-07-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多