嗨,我正在练习 plist,我了解到有 2 种不同的方式来加载它们
第一种方法:
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);