Loading an array from .Plist

I am trying to load my array from an array in my .Plist, but its not working.

Plis looks like this:

enter image description here

This is the code I'm using:

NSString *path = [[NSBundle mainBundle]pathForResource:@"DiseasePropertyList" ofType:@"plist"]; NSMutableArray *rootLevel = [[NSMutableArray alloc]initWithContentsOfFile:path]; self.myArray = rootLevel; [rootLevel release]; 
+6
source share
5 answers

Try it. Please change the file name. It works great.

 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *recentFilePath = [documentsDirectory stringByAppendingPathComponent:@"recent.plist"]; NSArray *history = [NSArray arrayWithContentsOfFile:recentFilePath]; 
+9
source

Your plist is really a dictionary. An array is the object of this dictionary for the key "Array". You can make plist an array in Xcode 4 by selecting "Array" and cut (CMD-x), and then pasting into an empty plist (CMD v).

+5
source
 NSString *pathStr = [[NSBundle mainBundle] bundlePath]; NSString *finalPath = [settingsBundlePath stringByAppendingPathComponent:@"yourproject.plist"]; NSDictionary *settingsDict = [NSDictionary dictionaryWithContentsOfFile:finalPath]; 

and just check the description of settingsDict. Hope this helps you.

+2
source

If your plist file is in your application, the easiest:

 NSArray *myArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"filename_without_extension" ofType:@"plist"]]; 
+2
source

Use this method, it worked fine for me.

 NSArray *country= [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"CountryList" ofType:@"plist"]]; 
0
source

Source: https://habr.com/ru/post/894504/


All Articles