我从自定义plist中读到了一个问题。我找到了Apple文档,以及这里和其他地方的很多帖子,但我无法理解我做错了什么。
我的plist是一个包含两个字符串(url)的字典。
这是我的代码,就像Apple和其他网站一样:
- (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);
}
这是日志....我不明白为什么serverURL(合成的字符串变量)是'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)
我在viewDidLoad
中使用serverURL之前完成了最后一次检查我在构建设置中添加了plist(我认为)。它一定是我的代码的问题,但我找不到。
答案 0 :(得分:0)
可以更轻松地将plist
加载到NSDictionary
NSMutableDictionary *plistDictionary = [[NSMutableDictionary alloc] initWithContentsOfFile: plistPath];
试试。