vendredi 23 juillet 2021

How to convert to Codable in Swift?

Organization is a Codable class:

final class Organization: Model, Content {
    static let schema = "Organization"
    
    @ID(custom: "id", generatedBy: .random)
    var id: UUID?

I have this method:

func aaa(obj: Any, keyPath: [String], value: Any) {
    if keyPath.count == 1 {
        let key = keyPath.first
        if let o = obj as Organization {
            o.setValue(value, forKey: key)
        }
        
    } else {
        var keyPath2 = keyPath
        let key = keyPath2.removeFirst()
        let t = type(of: obj)
        let mirror = Mirror(reflecting: t)
        let obj2 = mirror.children.filter{$0.label == key}.first?.value
        aaa(obj: obj2!, keyPath: keyPath2, value: value)
    }
}

4th line

if let o = obj as Organization {

raise

'Any' is not convertible to 'Organization'

Do you have any idea how to fix? How to pass obj into aaa method? Is it any more high level type than Any?





Aucun commentaire:

Enregistrer un commentaire