简体   繁体   English

如何存储 [[string]] 并从核心数据中获取它?

[英]how to store [[string]] and get it from core data?

I have sample array of [[string]] like this [["A", "B", "C"], ["A"], ["B", "C"], ["C"], ["B"], ["B", "C", "A"]]我有这样的 [[string]] 示例数组 [["A", "B", "C"], ["A"], ["B", "C"], ["C"], [ "B"], ["B", "C", "A"]]

I store it to core data like this:我将它存储到这样的核心数据中:

insertHeroes.setValue(roless, forKey: "roles")

which roless is array result from API (after append loop) with var roless = [[String]]()其中角色是来自 API(在 append 循环之后)的数组结果,其中var roless = [[String]]()

core data is stored in list var coreHeroList: [NSManagedObject] = []核心数据存储在列表中var coreHeroList: [NSManagedObject] = []

but when I try to get from core data and store it (after removeall) like this:但是当我尝试从核心数据中获取并存储它(在removeall之后)时:

if (coreHeroList.count > 0) {
            for hero in coreHeroList {
                roless.append(hero.value(forKey: "roles") as? [[String]] ?? [[""]])
            }
        }

it give red error: Cannot convert value of type '[[String]]' to expected argument type '[String]'它给出红色错误:无法将类型“[[String]]”的值转换为预期的参数类型“[String]”

My transformable is like this:我的变形金刚是这样的: 在此处输入图像描述

I set core data to manual and already set NSManagedObject SubClass我将核心数据设置为手动并已设置 NSManagedObject SubClass

How to get [[string]] from core data?如何从核心数据中获取 [[string]]? Does my insert to core data wrong?我对核心数据的插入是否错误?

I think the [[String]] is being stored in CoreData correctly.我认为[[String]]正确地存储在 CoreData 中。 The problem is that you are trying to append it directly to roless : you can append things of type [String] , but you can't append things of type [[String]] .问题是您正在尝试将roless直接转换为角色:您可以使用[String]类型的 append 事物,但不能[[String]]类型的append事物。 You can, however, merge them:但是,您可以合并它们:

roless += hero.value(forKey: "roles") as? [[String]] ?? [[""]]

But I agree with @JoakimDanielson's comment: it might be wise to create a Role entity and model a to-many relationship rather than handle transformables.但我同意@JoakimDanielson 的评论:创建Role实体和 model 可能是明智的,而不是处理可转换的关系。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM