Objective-c加载和写入plist-错误
嗨,我在练习plists,我了解到有两种不同的加载方式 第一种方法:Objective-c加载和写入plist-错误,objective-c,file,plist,Objective C,File,Plist,嗨,我在练习plists,我了解到有两种不同的加载方式 第一种方法: NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documents = [path lastObject]; NSString *filePath = [documents stringByAppendingPathComponent:@"test.plist"]
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documents = [path lastObject];
NSString *filePath = [documents stringByAppendingPathComponent:@"test.plist"];
self.array = [NSArray arrayWithContentsOfFile:filePath];
NSString *filePath = [[NSBundle mainBundle]pathForResource:@"Ingredients" ofType:@"plist"];
self.array = [NSArray arrayWithContentsOfFile:filePath];
第二种方法:
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documents = [path lastObject];
NSString *filePath = [documents stringByAppendingPathComponent:@"test.plist"];
self.array = [NSArray arrayWithContentsOfFile:filePath];
NSString *filePath = [[NSBundle mainBundle]pathForResource:@"Ingredients" ofType:@"plist"];
self.array = [NSArray arrayWithContentsOfFile:filePath];
我不清楚哪种方式最好。。。但我注意到,如果我使用第二个,我就不能在plist中写字。有人能告诉我更多关于它的情况吗?哪一种方法是最好和正确的?有什么区别
我正在做一些测试,我有一些代码只使用一种方法
//using this code the nslog will print null
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *filePath = [documentsPath stringByAppendingPathComponent:@"Ingredients.plist"];
ingredients = [NSMutableArray arrayWithContentsOfFile:filePath];
NSLog(@"ingredients:%@", self.ingredients);
//using this code the nslog will print the content of the array
NSString *filePath = [[NSBundle mainBundle]pathForResource:@"Ingredients" ofType:@"plist"];
ingredients = [NSMutableArray arrayWithContentsOfFile:filePath];
NSLog(@"Authors:%@", self.ingredients);
第一种方法 您的应用程序仅(在非越狱设备上)在“沙盒”环境中运行。这意味着它只能访问自己内容中的文件和目录。例如文档和库 参考资料 要访问应用程序沙箱的文档目录,可以使用以下命令:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
此文档目录允许您存储应用程序创建或可能需要的文件和子目录
要访问应用程序沙盒的库目录中的文件,请使用(代替上面的路径
):
第二种方法
第二种方法用于将文件写入应用程序主捆绑包。
主捆绑包是包含运行应用程序的代码和资源的捆绑包。如果您是应用程序开发人员,这是最常用的捆绑包。主捆绑包也是最容易检索的,因为它不需要您提供任何信息
最好将文件从应用程序主捆绑包复制到应用程序文档目录,然后使用文档目录路径读取/写入文件
如果使用第一种方法,则需要将文件从主要资源复制到文档目录。
将文件从应用程序包复制到应用程序的文档目录的代码
#define FILE_NAME @"sample.plist"
// Function to create a writable copy of the bundled file in the application Documents directory.
- (void)createCopyOfFileIfNeeded {
// First, test for existence.
BOOL success;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:FILE_NAME];
success = [fileManager fileExistsAtPath:filePath];
if (success){
return;
}
// The writable file does not exist, so copy the default to the appropriate location.
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:FILE_NAME];
success = [fileManager copyItemAtPath:defaultDBPath toPath:filePath error:&error];
if (!success) {
NSAssert1(0, @"Failed to create writable file with message '%@'.", [error localizedDescription]);
}
}
1)标题中提到的“错误”在哪里2) 这两种方法仅在plist文件的位置上有所不同。如果你说出你不了解的所用方法文档的哪一部分,人们会更乐意提供帮助。正确的方法是适合你的方法,并且以最有感情的方式做你想做的事情。因此,如果你不能使用第二种方法向plist写入数据,并且你想这样做,那么显然这不是正确的方法,使用第一种方法将是最有效的。如果您不想写入文件,那么第二个将和第一个一样有效。一些开发人员说代码越少越好(情况并非总是如此),因此,如果您采用这种方法,那么最好使用第二种方法。最后,是否有一个实际的错误,如标题所说,或者标题是错误的?错误是,当我在数组中加载plist并尝试打印时,它只能用第二种方法打印,但这很糟糕,因为我不能用这种方式在plist中写入…好吧,现在很清楚为什么第二种方法不允许我写入。。。但是你能解释一下为什么我的代码不起作用吗?我刚刚编辑了我的帖子