ios - 向 Core Data Swift 3 中插入​​两行数据

标签 ios arrays swift core-data

我有一个核心数据堆栈,该堆栈由一个名为 Device 的实体设置,该实体具有两个属性,asset_tag 和 location。我有两个这样设置的数组:

var assetTag = ["53","35","26","42","12"]
var location = ["SC", "FL", "NA", "NY", "CF"]

我的代码的第一部分循环遍历第一个数组并将每个数字添加到 asset_tag 属性中,如下所示:

let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext

  for device in assetTag {

        let newArray = NSEntityDescription.insertNewObject(forEntityName: "Device", into: context)

        newArray.setValue(device, forKey: "asset_tag")
    }

这会将每个值添加到一个数组中,这样我可以稍后将它们打印出来,而且效果很好。我想对第二个数组做同样的事情并将它们添加到我的第二个属性但是当我尝试时,它没有正确添加数据。这是我的:

for locations in location {

            let secondArray = NSEntityDescription.insertNewObject(forEntityName: "Device", into: context)

            secondArray.setValue(locations, forKey: "location")

        }

当我打印出结果时,我得到了一堆空值:

[<Device: 0x600000281130> (entity: Device; id: 0xd000000001580000 <x-coredata://22AC91EB-92B1-4E5B-A5A9-A5924E0ADD3E/Device/p86> ; data: {
    "asset_tag" = nil;
    devices = nil;
    location = CF;
}), <Device: 0x60000009f040> (entity: Device; id: 0xd0000000015c0000 <x-coredata://22AC91EB-92B1-4E5B-A5A9-A5924E0ADD3E/Device/p87> ; data: {
    "asset_tag" = 53;
    devices = nil;
    location = nil;
}), <Device: 0x6000002810e0> (entity: Device; id: 0xd000000001600000 <x-coredata://22AC91EB-92B1-4E5B-A5A9-A5924E0ADD3E/Device/p88> ; data: {
    "asset_tag" = nil;
    devices = nil;
    location = NY;

我不确定那些 nils 是从哪里来的。

编辑:

这看起来更好,但我得到了另一个名为 Devices with nils 的属性:

  "asset_tag" = 12;
    devices = nil;
    location = CF;

此外,当我像上面那样打印出核心数据的结果时,值的顺序与我定义数组的顺序不同。

"asset_tag" = 12;
    devices = nil;
    location = CF;

  "asset_tag" = 53;
    devices = nil;
    location = SC;

上面的结果显示 12,然后是 53,然后是 CF,然后是 SC,但这与设置的原始数组的顺序不同。

最佳答案

您必须创建 5 个具有 2 个属性的实例。您的代码将创建 10 个不同的实例。 Core Data 实体就像一个具有多个属性的类。

解决方案是使用基于索引的循环并分配两个属性。

为了更好地理解,将 Core Data 实例命名为 device 而不是 newArray。以下代码使用数组来保存实例。

assert 行用于检查两个数组是否包含相同数量的项目。

var devices = [Device]()

let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext

assert(assetTag.count == location.count, "Both arrays must have the same number of items")
for i in 0..<assetTag.count {

    let device = NSEntityDescription.insertNewObject(forEntityName: "Device", into: context) as! Device

    device.asset_tag = assetTag[i]
    device.location = location[i]
    devices.append(device)
}

边注:

在 Core Data 模型中声明应该具有非可选值的字符串属性。

关于ios - 向 Core Data Swift 3 中插入​​两行数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44490968/

相关文章:

javascript - 更改 NativeScript TabView 默认行为

java - 组合字符串数组和迭代器并排序

ios - 是否可以将 Decodable 与 Firestore 时间戳/日期一起使用?

ios - Swift:阻止滚动内的图像上下移动

ios - 绑定(bind)到 iTunes Connect 的开发者帐户 - 为另一个组织发布?

objective-c - UITextView 子类作为自身的委托(delegate)

java - 当数组大小未知时,在 for 循环中抛出数组越界异常(乘法持久性)

java - 使用 BufferedReader 进行递归

swift - 如何让一个方法出现在 "overload set"只有当另一个方法可以被调用时?

xcode - 一行上的连续声明必须在 for 循环中以 ';' 分隔,即使已经包含在内