【问题标题】:iphone - coredata application directoryiphone - coredata 应用程序目录
【发布时间】:2011-01-14 21:05:17
【问题描述】:

我将这个 Xcode 模板用于核心数据。

在委托上,我有这个方法:

- (NSURL *)applicationDocumentsDirectory {
  return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}

但这需要 iOS4。

我正在尝试使该应用与 3.0 兼容。

所以,我已将此方法转换为:

- (NSURL *)applicationDocumentsDirectory {

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

}

但是当 coredata 尝试使用商店 URL 时

  NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"MyFile.sqlite"];
  if (![persistentStoreCoordinator_ addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {

它在 IF 行崩溃并显示以下消息:

CoreData SQL 存储仅支持文件 URL(获取 /var/mobile/Applications/BB312A7E-6AE1-4BA2-AD87-6B96D8855CC6/Documents/MyFile.sqlite)。'

但 StoreURL 是一个 NSURL!

有什么线索吗?谢谢

【问题讨论】:

    标签: iphone core-data


    【解决方案1】:

    尝试做:

    NSString *pathToYourSqliteFile = @"/your/path/here";
    NSURL *storeURL = [NSURL fileURLWithPath: isDirectory:NO];
    

    看看这能不能解决问题。

    【讨论】:

    • 我的应用程序部署目标是 8.0,这对它有用!谢谢!
    【解决方案2】:

    CoreData application directory crashes on iOS3

    Surfdev 的建议在 3.1.2 上运行良好

    【讨论】:

      【解决方案3】:

      斯威夫特 3

       let documentsUrl = FileManager.default.urls(for: .documentDirectory, in:.userDomainMask).first!
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-02-16
        • 2023-03-23
        相关资源
        最近更新 更多