我很高兴与Plists和IOS合作。我正在尝试加载记录的tableview,我正在尝试从项目中的documents文件夹中加载Tableview。它曾经从bundle加载然后我指向documents文件夹。我从我的项目中删除了plist,并将其保存在文档文件夹中,但是我收到了一个副本错误:所以,请查看我的代码并告诉我如何从我的应用程序中的documents文件夹加载。我在调试中运行它,它仍然只加载包中plist的5条记录,而不是我文档文件夹中的7条记录。请注意,在我加载tableView记录的初始屏幕后,我选择" +"在顶部加载一个空白屏幕加载另一条记录,这我写入文档文件夹,它的工作原理,现在我想在Master上显示这些更改。我也不确定如何刷新屏幕。我已经阅读了一些关于" ViewDidLoad",但我不确定如何使用这种方法。提前谢谢。
此致
NSLog(@"loading data");
self.navigationItem.title=@"Accounts";
// NSString *accountFile = [[NSBundle mainBundle] pathForResource:@"Accounts2" ofType:@"plist"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentPath = [paths objectAtIndex:0];
NSString *plistPath = [documentPath stringByAppendingPathComponent:@"Accounts2.plist"];
accounts = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
account = [accounts objectForKey:@"Account"];
number = [accounts objectForKey:@"Number"];
dueDay = [accounts objectForKey:@"DayDue"];
minAmount = [accounts objectForKey:@"MinAmount"];
balance = [accounts objectForKey:@"Balance"];
NSLog(@"data loaded");
答案 0 :(得分:0)
在捆绑中没有plist
的第一个常见问题是如何将它们复制到文档目录中。所以请将你的plist
文件保存到Bundle,然后复制到文档目录,如下面的代码: -
BOOL success;
NSError *error;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"yourPlistName.plist"];
success = [fileManager fileExistsAtPath:filePath];
if (success) return;
NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingFormat:@"yourPlistName.plist"];
success = [fileManager copyItemAtPath:path toPath:filePath error:&error];
if (!success) {
NSAssert1(0, @"Failed to copy Plist. Error %@", [error localizedDescription]);
}
您可以使用以下代码行获取此plist文件: -
NSString *path = filePath;
NSMutableDictionary *plistdictionary = [[NSMutableDictionary alloc]initWithContentsOfFile:path];
您可以直接从资源(NSBundle)获取plist文件,如下面的代码: -
NSString* plistPath = [[NSBundle mainBundle] pathForResource:@"yourPlistName" ofType:@"plist"];
NSMutableDictionary *plistdictionary = [[NSMutableDictionary alloc]initWithContentsOfFile:plistPath];