【发布时间】:2014-07-31 14:50:25
【问题描述】:
您好,我有以下完美运行的代码,它会找到 Plist 并将 NSDictionary 分配给我的名为landingSites 的NSArray。唯一的问题是我希望用户能够添加站点,因此需要将 Plist 存储在用户文档中,而不是 NSBundle:
plistPath = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"];
NSMutableDictionary *newDictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
dictionaryKeys = [newDictionary allKeys];
self.landingSites = [dictionaryKeys sortedArrayUsingSelector:@selector(compare:)];
self.dictionaryFromPlist = newDictionary;
[Picker selectRow:0 inComponent:0 animated:NO];
for(NSString *parent in dictionaryKeys)
{
NSDictionary *parentData = [dictionaryFromPlist objectForKey:parent];
NSArray *child = [parentData objectForKey:@"Name"];
NSLog(@"%@", child);
}
到目前为止,我已经生成了下面的代码,但是每当我运行程序时,if 语句总是会执行,就好像 plist 从未复制到用户文档一样。另外,我不知道如何像上面那样将NSDictionary 内容附加到我的landingSites 数组中。请帮忙
(void) viewDidLoad {
[super viewDidLoad];
NSError *error;
NSArray *paths =NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask,YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"Data.plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:path]) {
NSLog(@"copying database to users documents");
NSString *pathToSettingsBundle = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"];
[fileManager copyItemAtPath:pathToSettingsBundle toPath:path error:&error];
}
else{
NSLog(@"users database already configured");
}
【问题讨论】:
-
您是否检查过
Data.plist不存在于Documents目录中?如果fileExistsAtPath:正在返回YES,那么这是有原因的。
标签: ios objective-c xcode plist nsbundle