Reputation: 492
I have a plist file generated by system_profiler -xml
command:
How can I get to that cpu_type
or machine_name
and store them in NSString
s?
I tried many methods, even converting this file into JSON, but couldn't get to those elements.
Example:
str
is nil
The path seems to be something like:
root \ 0 \ _items \ 0 \ cpu_type
root \ 0 \ _items \ 0 \ current_processor_speed
...
There is no enumeration here, so I should be able to get straight to those keys, but I don't know how.
Thank you!
Upvotes: 0
Views: 366
Reputation: 5745
[arr valueForKey:@"cpu_type"]
will not work because arr
is an array, not a dictionary. To access the value you want, try this
NSDictionary *rootDictionary = [NSArray arrayWithContentsOfFile:filePath][0];
NSDictionary *itemsDictionary = rootDictionary[@"_items"][0];
NSString *CPUType = itemsDictionary[@"cpu_type"];
Note that [0]
is shorthand for [array objectAtIndex:0]
, and [@"key"]
is short for [dictionary objectForKey:@"key"]
Getting machine_name
would be done in the same fashion
NSString *machineName = itemsDictionary[@"machine_name"];
Upvotes: 1