Ios 为什么我的Plist是从bundle而不是文档库加载的

Ios 为什么我的Plist是从bundle而不是文档库加载的,ios,xcode,Ios,Xcode,我对使用Plists和IOS非常陌生。我正在尝试加载记录的tableview,并且正在尝试从项目中的“文档”文件夹加载tableview。它通常从包中加载,然后我指向documents文件夹。我从我的项目中删除了plist,并将其保存在documents文件夹中,但我收到了一个复制错误:因此,请查看我的代码,并告诉我如何从我的应用程序的documents文件夹中加载。我在debug中运行了它,它仍然只加载捆绑包中plist中的5条记录,而不是驻留在documents文件夹中的7条记录。请注意,在

我对使用Plists和IOS非常陌生。我正在尝试加载记录的tableview,并且正在尝试从项目中的“文档”文件夹加载tableview。它通常从包中加载,然后我指向documents文件夹。我从我的项目中删除了plist,并将其保存在documents文件夹中,但我收到了一个复制错误:因此,请查看我的代码,并告诉我如何从我的应用程序的documents文件夹中加载。我在debug中运行了它,它仍然只加载捆绑包中plist中的5条记录,而不是驻留在documents文件夹中的7条记录。请注意,在加载tableView记录的初始屏幕后,我在顶部选择“+”以加载一个空白屏幕以加载另一条记录,我正在将其写入文档文件夹,它可以工作,现在我希望在主控上显示这些更改。我也不确定如何刷新屏幕。我读过一些关于“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");

第一个常见的问题是,如果捆绑包中没有
plist
,如何将它们复制到文档目录中。?因此,请将您的
plist
文件打包,然后复制到文档目录中,如下面的代码:-

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]);
}
NSString* plistPath = [[NSBundle mainBundle] pathForResource:@"yourPlistName" ofType:@"plist"];
NSMutableDictionary *plistdictionary = [[NSMutableDictionary alloc]initWithContentsOfFile:plistPath];
您可以使用以下代码行获取此plist文件:-

NSString *path = filePath;
NSMutableDictionary *plistdictionary = [[NSMutableDictionary alloc]initWithContentsOfFile:path];
您可以直接从资源(NSBundle)获取plist文件,如下面的代码:-

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]);
}
NSString* plistPath = [[NSBundle mainBundle] pathForResource:@"yourPlistName" ofType:@"plist"];
NSMutableDictionary *plistdictionary = [[NSMutableDictionary alloc]initWithContentsOfFile:plistPath];

您的应用程序包中有一个plist,您正在从中加载数据。您将plist中的数据保存到应用程序文档目录的何处?这是一个不断出现的问题。你可以参考一个类似的和一个类似的例子。它也在做同样的事情。只是区别在于它保存的是“笔记”而不是“帐户”。谢谢您的回复。我相信这个问题被问了很多,对于我们这些理解编程的人来说,从一个地方阅读并更新另一个地方是有点困惑的。