【问题标题】:resourcePath not working Xcode资源路径不工作Xcode
【发布时间】:2012-07-12 07:32:37
【问题描述】:

我为我的应用创建了一个包含数据的 sqlite 数据库,并将其拖到 Xcode 中的支持文件组中。

当我尝试使用以下方式访问它时:

NSString *bundlePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"database.db"];

它不读取文件。但是,如果我输入完整的文件路径:

NSString *bundlePath = @"Users/xxxx/Apps/App/database.db";

该应用运行良好。


我从以下位置打印出文件路径:

NSString *bundlePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"database.db"];

它是:

/Users/xxx/Library/Application Support/iPhone Simulator/5.1/Applications/EE614EFA-BAF9-478E-xxxx-xxxx/App.app/database.db

有什么想法吗?

【问题讨论】:

    标签: iphone ios xcode ios5


    【解决方案1】:

    处理路径的最佳方式是让 API 处理有关将内容“附加”到路径字符串的细节。

    NSString *path = [[NSBundle mainBundle] pathForResource:@"database" 
                                                     ofType:@"db"]; 
    

    【讨论】:

    • 当我尝试这样做时,路径评估为 nil。
    • 在 Xcode Project Navigator(左侧)中选择文件,并确保在 File Inspector(右侧)的“Target Membership”下选中您的目标。 -- 还要仔细检查文件名的拼写。
    【解决方案2】:

    NSString *path = [[NSBundle mainBundle] pathForResource:@"database" ofType:@".SQLite"];

    【讨论】:

      猜你喜欢
      • 2013-10-02
      • 2013-06-10
      • 2012-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-27
      • 1970-01-01
      • 2016-01-11
      相关资源
      最近更新 更多