从核心数据填充元组

时间:2019-05-06 15:24:20

标签: swift core-data tuples

我不明白如何用来自核心数据的数据填充元组摄像机
我只填写核心数据中的最后一个值

  override func viewDidLoad() {
    super.viewDidLoad()
    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    let context = appDelegate.persistentContainer.viewContext

    let entity = NSEntityDescription.entity(forEntityName: "MarkersModel", in: context)


    let request = NSFetchRequest<NSFetchRequestResult>(entityName: "MarkersModel")

    request.returnsObjectsAsFaults = false

    do {
        let result = try context.fetch(request)
        for data in result as! [NSManagedObject] {
            print(data.value(forKey: "nameCam") as! String)
            print(data.value(forKey: "urlCam") as! String)

            cameras = [(data.value(forKey: "nameCam") as! String, data.value(forKey: "urlCam") as! String)]

        }
    } catch {
        print("Failed")
    }}

1 个答案:

答案 0 :(得分:0)

您需要为从Core Data获取的每个对象附加一个新的元组。为了避免使用valueForKey,我修改了提取请求声明

let request: NSFetchRequest<MarkersModel> = MarkersModel.fetchRequest()
request.returnsObjectsAsFaults = false

do {
    let result = try context.fetch(request)
    for data in result {
        print(data.nameCam)
        print(data.urlCam)

        cameras.append((data.nameCam, data.urlCam))
    }
} catch {
    print(error)
}}