I was creating a data structure manually using the following:
NSDictionary* league1 = [[NSDictionary alloc] initWithObjectsAndKeys: @\"Barclays Premier Leagu
NSString* plistPath = [[NSBundle mainBundle] pathForResource:@"league" ofType:@"plist"];
contentDict = [NSDictionary dictionaryWithContentsOfFile:plistPath];
That answer is correct - are you sure that your file is in the app? Did you add it to your project, and check to see if it gets copied into your app bundle? If not, it might be the file was not added to the target you are building, an easy mistake to make especially if you have multiple targets.
For completeness, Kendall and bentford are completely correct. However, in my sample, contentArray was a property and by the end of the method it was going out of scope because arrayWithContentsOfFile creates an auto-released object.
To make this work correctly I needed to do 3 things:
put the file in the resources folder
name the file correctly (was leagues.plist instead of league.plist)
read the file using [[NSArray alloc] initWithContentsOfFile:plistPath)];
the third part creates an allocated NSArray that does not release when you exit the scope of this function... of course, this needed to be released in the dealloc function.
Just to add. I had the same problem and the suggested solution helped me solve the problem, however I am not sure if I actually used the exact solution. In my case the problem was that the .plist file was added to a different target (had added a new target a moment before). Therefore the solution was .plist > Get Info > Targets, and make sure it is added to the correct target so it gets copied to device when installing. Darn had I figure that out soon enough I would have saved a lot of time. Hope this is helpful too. Regards!
Kendall is correct.
In my experience, you need to add your file to the "Resources" folder in xcode.
I had this issue but it wasn't working because I was putting the results from the pList into an array where it should have been a dictionary, i.e
NSString* plistPath = [[NSBundle mainBundle] pathForResource:@"VehicleDetailItems" ofType:@"plist"];
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
Use this code if the plist
is in the resources folder of the project.
NSString *sourcePath = [[NSBundle mainBundle] pathForResource:@"league" ofType:@"plist"];
contentArray = [NSArray arrayWithContentsOfFile:sourcePath];
If the plist
is inside the document directory of the app use this:
NSArray *paths =
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
NSString *plistName = @"league";
NSString *finalPath = [basePath stringByAppendingPathComponent:
[NSString stringWithFormat: @"%@.plist", plistName]];
contentArray = [NSArray arrayWithContentsOfFile:finalPath];