使用通用文件路径加载属性列表?

时间:2014-01-23 09:17:26

标签: ios objective-c property-list

我之前正在阅读Plist,我看到了这段代码:

       [super viewDidLoad];
       NSString *path = [[NSBundle mainBundle] pathForResource:@"projects" ofType:@"plist"];
       projects = [NSArray arrayWithContentsOfFile:path];
       }

现在我已经手动输入我的属性列表的文件路径,我很好奇这是普遍找到文件路径的正确方法吗?

如果是这样,有人可以向我解释一下它是如何运作的,谢谢。

2 个答案:

答案 0 :(得分:1)

是的,用于从主资源包中获取文件。

要从您的应用程序文档目录获取资源路径,您可以尝试

/**
 Returns the URL to the application's Documents directory.
 */
- (NSURL *)applicationDocumentsDirectory
{
    return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}

可以像

一样使用
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"yourFile.ext"];

答案 1 :(得分:0)

是的,这是正确的方法,也是自我解释的。第一步,你找到了plist文件的路径,然后你用plist文件的内容填充一个数组。如果您使用可变数组而不是数组,您还可以编辑plist文件并保存回来