简体   繁体   中英

I can read from a plist but fail to read from another one

I created two plist file named information.plist and data.plist in my Supporting Files, and I add both them to Copy Bundle Resource.

I use the code below to read data from the plist.

NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory , NSUserDomainMask , YES );
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"data.plist"];
NSMutableDictionary* dictPlist = [ [ NSMutableDictionary alloc ] initWithContentsOfFile:filePath];

and try to show data from the plist:

NSMutableDictionary *data1 = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
NSLog(@"%@", data1);

I get the data from information.plist but get "null" from data.plist.

Did I miss something?

Thanks for your helping!

Try this:

// Path of plist from bundle
NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"data" ofType:@"plist"];

// Since your plist's root is a dictionary else you should form NSArray from contents of plist
NSDictionary *dataPlist = [NSDictionary dictionaryWithContentsOfFile:plistPath];

Explanation : Your plists are in application bundle and not inside Documents directory which is why you are not getting them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM