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

156 views Asked by At

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 : enter image description here

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?

1

There are 1 answers

1
pbasdf On

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.