微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何将Core Data添加到现有的Xcode 9 Swift 4 iOS 11项目中?

请求更新,因为此问题当然已针对先前版本得到解答,最新的搜索结果日期为12月16日与之前的iOS 9和10项目产生无关的兼容性.

文档当然是说在开始一个新项目时选择使用核心数据复选框,我没有选择,但现在认为需要添加iCloud核心数据以使我的应用程序进入下一阶段 – >其中需要NSFileCoordinator和NSFilePresenter之类的东西,因为在我的应用UI中,用户会看到一些TOPICS,每个都有三个OPTIONS,用户可以选择一个选项.对于每个主题,UI然后显示已选择每个选项的总用户数和每个选项的总计PERCENTAGE.

现在,每个选项的选择数量和总数的百分比当然只是在我的原生应用程序中计算的 – >但实际上需要在像云一样的中心或者很可能在网站上进行计算…但是网站引发了NSFileCoordinator和NSFilePresenter已经解决的同时读/写问题.

因此,如果iCloud核心数据系统可以插入现有的泛在容器数值总计的基本算术计算 – 在接收来自各个用户的写入数值命令时在云中 – 在发出新的无处不在的容器数值总和和百分比值之前 – 那么我’非常感谢建议在尝试创建和初始化核心数据堆栈时如何修复下面生成错误.否则猜测我将不得不刮掉Xcode并转到像PhoneGap这样的混合应用程序,如果这是最好的工作.

因此,请参阅核心数据编程指南:

https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/CoreData/InitializingtheCoreDataStack.html#//apple_ref/doc/uid/TP40001075-CH4-SW1

并在我现有项目的开头粘贴以下代码,生成

Use of unresolved identifier ‘persistentContainer’… ‘managedobjectContext’

……错误.和线

init(completionClosure: @escaping () -> ()) {

生成

Initializers may only be declared within a type

import UIKit

import CoreData
class DataController: NSObject {
  var managedobjectContext: NSManagedobjectContext
  init(completionClosure: @escaping () -> ()) {
    persistentContainer = NSPersistentContainer(name: "DataModel")
    persistentContainer.loadPersistentStores() { (description,error) in
      if let error = error {
        fatalError("Failed to load Core Data stack: \(error)")
      }
      completionClosure()
    }
  }
}

init(completionClosure: @escaping () -> ()) {
  //This resource is the same name as your xcdatamodeld contained in your project
  guard let modelURL = Bundle.main.url(forResource: "DataModel",withExtension:"momd") else {
    fatalError("Error loading model from bundle")
  }
  // The managed object model for the application. It is a Fatal error for the application not to be able to find and load its model.
  guard let mom = NSManagedobjectModel(contentsOf: modelURL) else {
    fatalError("Error initializing mom from: \(modelURL)")
  }

  let psc = NSPersistentStoreCoordinator(managedobjectModel: mom)

  managedobjectContext = NSManagedobjectContext(concurrencyType: NSManagedobjectContextConcurrencyType.mainQueueConcurrencyType)
  managedobjectContext.persistentStoreCoordinator = psc

  let queue = dispatchQueue.global(qos: dispatchQoS.QoSClass.background)
  queue.async {
    guard let docURL = FileManager.default.urls(for: .documentDirectory,in: .userDomainMask).last else {
      fatalError("Unable to resolve document directory")
    }
    let storeURL = docURL.appendingPathComponent("DataModel.sqlite")
    do {
      try psc.addPersistentStore(ofType: NSsqliteStoreType,configurationName: nil,at: storeURL,options: nil)
      //The callback block is expected to complete the User Interface and therefore should be presented back on the main queue so that the user interface does not need to be concerned with which queue this call is coming from.
      dispatchQueue.main.sync(execute: completionClosure)
    } catch {
      fatalError("Error migrating store: \(error)")
    }
  }
}

// followed by my existing working code:

class ViewController: UIViewController {

解决方法

转到文件>新文件…在iOS下选择核心数据并选择数据模型
在项目创建期间选择核心数据时,您仍然需要一些xcode自动生成代码.
为了得到它,只需创建一个核心数据选项的新项目,并复制所有代码在** // Mark下编写: – AppDelegate.swift中的核心数据堆栈**注释
添加
import CoreData

以上

可选的

并且不要忘记在刚刚复制的代码的managedobjectModel函数中复制后更改应用程序的名称

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐