I am having an issue reading from a custom plist. I have found the Apple documentation, and many posts on here and elsewhere, but I cannot understand what I am doing wrong.
My plist is a dictionary with two strings (urls).
Here is my code that is just like Apple’s and other sites:
- (void)accessPlistForURLDictionary
{
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"server_urls.plist"];
if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath])
{
// if not in documents, get property list from main bundle
plistPath = [[NSBundle mainBundle] pathForResource:@"server_urls" ofType:@"plist"];
NSLog(@"bundle");
}
NSData *xml = [[NSFileManager defaultManager] contentsAtPath:plistPath];
NSString *error = nil;
NSPropertyListFormat format;
// convert static property liost into dictionary object
NSDictionary *plistDictionary = (NSDictionary *)[NSPropertyListSerialization propertyListFromData:xml mutabilityOption:NSPropertyListMutableContainersAndLeaves format:&format errorDescription:&error];
if (!plistDictionary)
{
NSLog(@"Error reading plist: %@, format: %d", error, format);
}
serverURL = [[plistDictionary objectForKey:@"Root"] objectForKey:@"get_firstview"];
NSLog(@"serverURL in accessPlist is: %@", serverURL);
}
Here is the log…. I don’t get why serverURL (a string variable that is synthesized) is ‘null’
2013-01-07 12:47:06.742 ME[2305:c07] bundle
2013-01-07 12:47:06.744 ME[2305:c07] serverURL in accessPlist is: (null)
2013-01-07 12:47:06.744 ME[2305:c07] serverURL is: (null)
The last check is done right before I use serverURL in viewDidLoad
I have the plist added in Build Settings (I think). It must be an issue with my code, but I can’t find what.
There is easier way to load
plistintoNSDictionaryTry that.