【问题标题】:Saving multiple objects for an attribute in CoreData在 CoreData 中为一个属性保存多个对象
【发布时间】:2018-11-04 08:35:49
【问题描述】:

CoreData 结构: Entity: ZooAnimals, Attributes: animal (String type) and count (Integer 16 type)

我想保存一些动物名称和每只动物的数量(计数)。我可以通过对执行此操作所需的多个对象进行硬编码来做到这一点——参见下面的代码——但是我怎样才能更灵活地做到这一点,以便我可以只给它几个任意长度的数组并将它们保存到 CoreData 中(例如 @ 987654323@)

import UIKit
import CoreData

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return }
        let managedContext = appDelegate.persistentContainer.viewContext

        let animal1 = NSEntityDescription.insertNewObject(forEntityName: "ZooAnimals", into: managedContext)
        let animal2 = NSEntityDescription.insertNewObject(forEntityName: "ZooAnimals", into: managedContext)
        let animal3 = NSEntityDescription.insertNewObject(forEntityName: "ZooAnimals", into: managedContext)
        let animal4 = NSEntityDescription.insertNewObject(forEntityName: "ZooAnimals", into: managedContext)

        animal1.setValue("Donkey", forKey: "animal")
        animal1.setValue(7, forKey: "count")
        animal2.setValue("Horse", forKey: "animal")
        animal2.setValue(3, forKey: "count")
        animal3.setValue("Zebra", forKey: "animal")
        animal3.setValue(9, forKey: "count")
        animal4.setValue("Okapi", forKey: "animal")
        animal4.setValue(2, forKey: "count")

        do {
            try managedContext.save()
        } catch let error as NSError {
            print("Could not save. \(error), \(error.userInfo)")
        }
    }
}

【问题讨论】:

标签: swift xcode core-data


【解决方案1】:

一个简单的解决方案是for 循环

let animalArray = ["Donkey","Horse","Zebra","Okapi"]
let animalCountArray = [7,3,9,2]
assert(animalArray.count == animalCountArray.count, "The number of items in the arrays must be equal")

for i in 0..<animalArray.count {
    let animal = NSEntityDescription.insertNewObject(forEntityName: "ZooAnimals", into: managedContext)
    animal.setValue(animalArray[i], forKey: "animal")
    animal.setValue(animalCountArray[i], forKey: "count")
}
do {
    try managedContext.save()
} catch  {
    print("Could not save. \(error)")
}

旁注:guarding AppDelegate 毫无意义。如果缺少应用程序委托类,应用程序甚至都不会启动。

【讨论】:

    【解决方案2】:

    只需使用 for 循环

    let employeeNames = ["Donkey", "Horse", "Zebra","Okapi"]
        for i in 0..<employeeNames.count
        {
            let employeeEntry = NSEntityDescription.insertNewObjectForEntityForName("ZooAnimals", inManagedObjectContext: managedObject)
    
            employeeEntry.setValue(employeeNames[i], forKey: "employeename")
            employees.setValue(index, forKey: "animal")
    
            do {
                try managedObject.save()
            } catch {
                print("problem saving")
            }
        }
    

    【讨论】:

    • 谢谢!这也有效,尽管以动物园动物的名字命名你的员工有点不寻常:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-27
    • 2021-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-19
    相关资源
    最近更新 更多