Convert ModelArray to Dictonary

PHOTO EMBED

Fri Jun 23 2023 08:10:31 GMT+0000 (Coordinated Universal Time)

Saved by @hasnat #ios #swift #dic #dict #model #modelarray #dictonary

Make sure to add encodable like this 

struct modelName : Encodsble

let categories = categoriesArray.map{ $0.toDictionary()  }

extension Encodable {
    func toDictionary() -> [String: Any] {
        let mirror = Mirror(reflecting: self)
        var dictionary = [String: Any]()
        for child in mirror.children {
            guard let label = child.label else {
                continue
            }
            dictionary[label] = child.value
        }
        return dictionary
    }
}
content_copyCOPY