2

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?

Sarimin
  • 707
  • 1
  • 7
  • 18

1 Answers1

0

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.

pbasdf
  • 21,386
  • 4
  • 43
  • 75
  • if I follow roless += hero.value(forKey: "roles") as? [[String]] ?? [[""]], it will print all empty array, but after I try with this roless.append(hero.value(forKey: "roles") as? [String] ?? [""]), it got all saved array value – Sarimin Apr 18 '20 at 13:49