【问题标题】:Bundle from Static LIbrary从静态库捆绑
【发布时间】:2013-04-09 11:31:52
【问题描述】:

我已将一个子项目作为静态库添加到我的主项目中。

现在,在主项目中,我尝试加载子项目包中的内容。像这样的:

NSString *defaultStorePath =
[[NSBundle bundleForClass:[self class]] pathForResource:@"database" ofType:@"sqlite"];

但这返回 nil...

我该如何解决?

【问题讨论】:

  • [NSBundle bundleWithIdentifier:...] 与您的子捆绑包的 CFBundleIndentifier 怎么样?
  • 我没有特别创建这个捆绑包。在哪里可以找到捆绑包标识符?简单的事情在 Xcode 上很复杂。
  • 是否将“database.sqlite”复制到应用程序包中?它究竟位于捆绑包内的什么位置?
  • database.sqlite 被复制到包中。它在 MyBundle/MyBundle/coredata/database.sqlite 里面
  • galloway.me.uk/tutorials/ios-library-with-resources 你可以尝试按名称初始化包吗? Xcode 不会自动为库创建资源包,请浏览您的主包内容(在设备上或iPhone Simulator 目录)以明确您在那里拥有哪些文件,注意iPhone Simulator 可能包含您已经拥有的文件从项目中删除。

标签: iphone ios ipad cocoa-touch cocoa


【解决方案1】:

A-live 给出了答案,但他太谦虚了,无法在此处发布作为答案。

这是答案的链接 http://www.galloway.me.uk/tutorials/ios-library-with-resources/

问题正是因为创建静态库时没有包含资源。

【讨论】:

    【解决方案2】:

    试试这样的..

        NSString *resourceBundlePath = [[NSBundle mainBundle]       
                       pathForResource:@"StaticLibrary" 
                                           ofType:@"bundle"];
    
        NSBundle *resourceBundle = [NSBundle bundleWithPath:resourceBundlePath];
    

    现在,您可以将此 resourceBundle 用作您的静态库包。

    【讨论】:

      【解决方案3】:

      我遇到了类似的问题,在我的情况下,甚至 bundleWithIdentifier 出于某种原因返回了 nil。如果没有其他工作,您可以随时尝试遍历所有文件,如下所示。这对我有用。

      [self urlForResource:@"database.sqlite"];
      
      // ...
      
      - (NSURL *)urlForResource:(NSString *)resource
      {
          NSFileManager *fileManager = [NSFileManager defaultManager];
          NSURL *directoryURL = [NSBundle bundleForClass:[self class]].bundleURL;
          NSDirectoryEnumerator *enumerator = [fileManager enumeratorAtURL:directoryURL includingPropertiesForKeys:nil options:0 errorHandler:^BOOL(NSURL *url, NSError *error) {
              return YES;
          }];
      
          for (NSURL *url in enumerator)
          {
              NSString *lastPathComponent = [[url absoluteString] lastPathComponent];
              if ([lastPathComponent isEqual:resource])
                  return url;
          }
          return nil;
      }
      

      【讨论】:

        猜你喜欢
        • 2015-01-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-01-20
        • 2022-01-09
        • 2017-08-31
        • 2020-02-26
        相关资源
        最近更新 更多