繁体   English   中英

为什么我的Plist是从捆绑包而不是文档库中加载的

[英]Why is my Plist loading from bundle rather than Document Library

我对使用Plists和IOS很陌生。 我正在尝试加载记录的表视图,并且试图从项目中的文档文件夹中加载表视图。 它过去是从捆绑软件中加载的,然后我指向了文件文件夹。 我从项目中删除了plist,并将其保留在documents文件夹中,但是收到复制错误:因此,请查看我的代码,并让我对如何从应用程序中的documents文件夹中加载有所了解。 我在调试中运行它,它仍然仅从捆绑包中的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文件保存到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];

您可以像下面的代码一样直接从Resource(NSBundle)获取plist文件:-

NSString* plistPath = [[NSBundle mainBundle] pathForResource:@"yourPlistName" ofType:@"plist"];
NSMutableDictionary *plistdictionary = [[NSMutableDictionary alloc]initWithContentsOfFile:plistPath];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM