Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method to get a value from plist

I have a integer called NumberX and a plist called PlistX.

Example

int NumberX;

PlistX:

plist

I was wondering how I'd make NumberX = value of Three

like image 445
akuritsu Avatar asked Apr 28 '12 05:04

akuritsu


2 Answers

Try this:

NSString *path = [[NSBundle mainBundle] pathForResource:@"PlistX" ofType:@"plist"];
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];
int number = [[[[dict objectForKey:@"One"] objectForKey:@"Two"]objectForKey:@"Three"] intValue];
NSLog(@"%d",number);    
like image 194
Hector Avatar answered Nov 22 '22 23:11

Hector


1) Get data (i.e. NSMutableDictionary *) from plist saved to disk:

NSMutableDictionary *dictionary =  [NSPropertyListSerialization propertyListFromData:data mutabilityOption:NSPropertyListMutableContainers format:NULL errorDescription:&error];

2) Retrieve needed object from dictionary.

//in your case
NSNumber *tmpNumber = [[[dictionary objectForKey:@"One"] objectForKey:@"Two"] objectForKey:@"Three"];
// convert to int
int NumberX = [tmpNumber intValue];
like image 40
Nekto Avatar answered Nov 22 '22 22:11

Nekto