Dynamically populate base object class from child data?

Using a system of inheritance between class objects I was hoping to be able to automatically populate an instance of the base class with any of its children, but I always have to manually type everything in.

class BaseClass: Codable {
    
    var name1: String
    var name2: String
    var name3: String
    
    init(name1: String, name2: String, name3: String) {
        
        self.name1 = name1
        self.name2 = name2
        self.name3 = name3
    }
}

class FirstClass: BaseClass {
    
    var name4: String
    var name5: String
    
    init(name1: String, name2: String, name3: String, name4: String, name5: String) {
        
        self.name4 = name4
        self.name5 = name5
    
    super.init(name1: name1, name2: name2, name3: name3)
    }
    
    required init(from decoder: Decoder) throws {
        fatalError("init(from:) has not been implemented")
    }
}

let test1: BaseClass = BaseClass(name1: "name1", name2: "name2", name3: "name3")
let test2: FirstClass = FirstClass(name1: "name1", name2: "name2", name3: "name3", name4: "name4", name5: "name5")

Is there an easy way to parse the data from "test2" to create an instance of "test1" similar to how Codable will just populate the data from JSON/Plist into an Object, only from one of its children?

Terms of Service

Privacy Policy

Cookie Policy