MariusP
MariusP

Reputation: 492

Access items from system_profiler plist

I have a plist file generated by system_profiler -xml command: plist file

How can I get to that cpu_type or machine_name and store them in NSStrings?

I tried many methods, even converting this file into JSON, but couldn't get to those elements.

Example:

Code

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

Answers (1)

Chris Loonam
Chris Loonam

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

Related Questions