Question

I have a plist file I just created of strings; it looks like this:

image of my plist file

This is the code I'm using to create the path to the file:

NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //  Create a list of paths
NSString *documentsDirectory = [paths objectAtIndex:0]; //  Get a path to your documents directory from the list
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"NailServices.plist"]; //  Create a full file path

NSFileManager *fileManager = [NSFileManager defaultManager];

if (![fileManager fileExistsAtPath: path]) { //  Check if file exists
    //  Get a path to the plist created before in bundle directory (by Xcode)
    NSString *bundle = [[NSBundle mainBundle] pathForResource: @"NailServices" ofType: @"plist"];
    [fileManager copyItemAtPath:bundle toPath: path error:&error]; //  Copy this plist to your documents directory
}

This is the code I'm using to examine the data (to make sure this is working)... I'm getting a (null) back from the NSLog statement)

//Load Dictionary with wood name cross refference values for image name
NSString *plistDataPath = [[NSBundle mainBundle] pathForResource:@"NailServices" ofType:@"plist"];
NSDictionary *NailServicesDictionary = [[NSDictionary alloc] initWithContentsOfFile:plistDataPath];

NSLog(@"\nnailServicesDict: %@", NailServicesDictionary);

This is my first attempt at creating/using a "strings" plist file; I have read everything I could find on Google and SO without finding an example of a plain ol' strings file. What else do I have to do to be able to get to this plist data?

Was it helpful?

Solution 2

As a commenter posted, plist files can either have an NSArray or an NSDictionary as their root. Your example plist has an NSArray as its root, so you'll want to alloc and init an NSArray, not an NSDictionary. If your plist is stored in the app bundle when you build the app in Xcode and you don't need to modify it at runtime, then it's unnecessary to copy it to the NSDocumentsDirectory. Also, I'd recommend using [paths lastObject]; rather than [paths objectAtIndex:0];, which can throw an exception if the paths array is empty.

OTHER TIPS

Your problem is that you are creating an NSDictionary while your plist is an NSArray. Thus, it will return nil when you try to create it as a dictionary because no dictionary exists.

You need to change:

NSDictionary *NailServicesDictionary = [[NSDictionary alloc] initWithContentsOfFile:plistDataPath];

to

NSArray *NailServicesArray = [[NSArray alloc] initWithContentsOfFile:plistDataPath];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top