从文档目录而不是 Bundle 获取 plist

发布于 2024-09-06 06:17:06 字数 626 浏览 4 评论 0原文

我试图指向文档目录而不是捆绑包,我有以下代码:

    - (id)initWithLibraryName:(NSString *)libraryName {
         if (self = [super init]) {
             libraryPlist = libraryName;
             libraryContent = [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] 
                                        pathForResource:libraryPlist ofType:@"plist"]];

我知道我需要使用以下命令将应用程序指向文档目录:

      NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
      NSString *documentsDirectory = [paths objectAtIndex:0];

我的问题是在上面的示例中在哪里插入此指针?

I'm trying to point to the documents directory instead of the Bundle, I have the following code:

    - (id)initWithLibraryName:(NSString *)libraryName {
         if (self = [super init]) {
             libraryPlist = libraryName;
             libraryContent = [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] 
                                        pathForResource:libraryPlist ofType:@"plist"]];

I know I need to point the app to the documents directory using the following:

      NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
      NSString *documentsDirectory = [paths objectAtIndex:0];

My question is where do I insert this pointer in the above example?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

呆° 2024-09-13 06:17:06
[[NSBundle mainBundle] pathForResource:libraryPlist ofType:@"plist"];

也是等价的:

[documentsDirectory stringByAppendingPathComponent:[libraryPlist stringByAppendingPathExtension:@"plist"]];

只不过如果文件不存在,bundle 调用将返回 nil。您可以在 NSFileManager 中使用 fileExistsAtPath: 来检查文件是否存在。

[[NSBundle mainBundle] pathForResource:libraryPlist ofType:@"plist"];

Is equivalent too:

[documentsDirectory stringByAppendingPathComponent:[libraryPlist stringByAppendingPathExtension:@"plist"]];

Except that the bundle call will return nil if the file does not exist. You can use fileExistsAtPath: in NSFileManager to check if a file exists.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文