简体   繁体   中英

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"]]

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]]()

core data is stored in list var coreHeroList: [NSManagedObject] = []

but when I try to get from core data and store it (after removeall) like this:

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]'

My transformable is like this: 在此处输入图像描述

I set core data to manual and already set NSManagedObject SubClass

How to get [[string]] from core data? Does my insert to core data wrong?

I think the [[String]] is being stored in CoreData correctly. 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]] . 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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