Convert array inside of a Swift Struct to NSdictionary

113 Views Asked by At

Below is my code which the DataModel struct is converted to a NSDictionary

struct DataModel: Decodable {
    let id: String
    let carID: String
    let ownerId: String
    let services : [Service]
    
    var dictionary : [String : Any] {
        return ["id": id,
                "carID": carID,
                "ownerID": ownerId,
                "services": services]
    }
    var nsDictionary : NSDictionary {
        return dictionary as NSDictionary
    }
}

struct Service: Decodable {
    let id: String
    let availableQuantity, usedQuantity: Int
    let discountedPrice: Double
    let discountPercent: Double?
}

I call the DataModel by doing response.nsDictionary

Inside the response my data is structured correctly so I can work with it in Objective C except for my Services array. The Service response looks like...

services = (“App.MyClassName.Service(id: \”112STT\”, availableQuantity: 1, usedQuantity: 1, discountedPrice: 20.0, discountPercent: Optional(0.0))"

How can I get this response data structured the same as my DataModel struct?

0

There are 0 best solutions below