繁体   English   中英

通过主捆绑包vs文档目录访问plist文件

[英]Accessing plist file by main bundle vs Documents directory

获得财产清单的最佳方法是什么?

1-来自主捆绑包,即:

NSString *path = [[NSBundle mainBundle] pathForResource:@"recipes"  ofType:@"plist"];

2-或从Documents目录获取路径,即:

// Data.plist code - get path from root direcory 
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES); 
// get documents path
NSString *documentsPath = [paths objectAtIndex:0]; 
// get the path to our plist file.
NSString *plistPath = [documentsPath 
stringByAppendingPathComponent:@"Data.plist"]; 
// check to see if Data.plist exists in documents 
if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath]) { 
    // if not in documents, get property list from main bundle 
    plistPath = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"]; 
}

什么,为什么? 如果两者都是正确的,那为什么我们只用一行代码就不使用第一个代码呢?

简单规则:

  • 如果属性列表文件是只读的,则将其放入包中。
  • 如果要更改属性列表文件请将其放在文档目录中。

暂无
暂无

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

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