Reputation: 1446
I have a struct
that I need to convert it into a Dictionary
struct ModelFeedbackServices: Codable {
let servicesId: String?
let servicesName: String?
let category: String?
}
And I have an array of the same type ModelFeedbackServices
private var selectedItems = [ModelFeedbackServices]()
and I need to convert this array into a Dictionary to add it in my parameters here:
var parameters: [String: Any]? {
var params: [String: Any] = [:]
switch self {
case .submit(let services, let comments):
////////////////////////////////////////////////////////////////////////////////////
params["FEEDBACK_SERVICES"] = services.asDictionary // Here it comes as nil!
////////////////////////////////////////////////////////////////////////////////////
params["COMMENTS"] = comments
return params
}
}
I have created extension Encodable
:
extension Encodable {
var asDictionary: [String: Any]? {
guard let data = try? JSONEncoder().encode(self) else { return nil }
return (try? JSONSerialization.jsonObject(with: data, options: .allowFragments)).flatMap { $0 as? [String: Any] }
}
}
The problem is every time I get a nil
value in services.asDictionary
inside the switch
statement
I need the dictionary looks like this
{
"FEEDBACK_SERVICES": [
{
"SERVICE_ID": "8",
"SERVICE_NAME": "ABC",
"CATEGORY": "1"
},
{
"SERVICE_ID": "4",
"SERVICE_NAME": "ABC",
"CATEGORY": "2"
}
],
"COMMENTS": "any comment"
}
Any idea would be helpful
Upvotes: 1
Views: 113
Reputation: 15248
You can introduce this Encodable
extension to get Dictionary
or Array
of Dictionary
typealias JSON = [String: Any]
extension Encodable {
/// Converting object to postable JSON
func toDictionary(_ encoder: JSONEncoder = JSONEncoder()) -> JSON {
guard let data = try? encoder.encode(self) else { return [:] }
guard let object = try? JSONSerialization.jsonObject(with: data, options: .allowFragments) else { return [:] }
guard let json = object as? JSON else { return [:] }
return json
}
/// Converting object to postable array of JSON
func toDictionaryArray(_ encoder: JSONEncoder = JSONEncoder()) -> [JSON] {
guard let data = try? encoder.encode(self) else { return [[:]] }
guard let object = try? JSONSerialization.jsonObject(with: data, options: .allowFragments) else { return [[:]] }
guard let jsonArray = object as? [JSON] else { return [[:]] }
return jsonArray
}
}
Usage
var parameters: [String: Any]? {
var params: [String: Any] = [:]
switch self {
case .submit(let services, let comments):
////////////////////////////////////////////////////////////////////////////////////
params["FEEDBACK_SERVICES"] = services.toDictionaryArray() // Here it comes as nil!
////////////////////////////////////////////////////////////////////////////////////
params["COMMENTS"] = comments
return params
}
}
Upvotes: 3