Reputation: 473
I am trying to pass a Parameters [String:Any] value to JSONSerialization.data and it's throwing error every time.
I know the values of the [String:Any] dictionary is Swifty.JSON objects. But I am not able to convert them to NSDictionary object.
I populate the params from another dictionary like so:
var params = [String:Any]()
for (key, value) in self.myDictionary[self.selectedEntry] as! JSON {
print("\(key) - \(value.description)")
params[key]=value
}
And this is whats inside the params object after a print(params).
["searchOptions": {
"omit_saved_records" : false
}, "customFilters": {
"is_next_sellers_search" : "Y"
}, "use_code_std": [
"RSFR",
"RCON"
]]
I am passing the params to this function:
let json = try JSONSerialization.data(withJSONObject: params, options: .prettyPrinted)
This is where the error occur.
I am expecting this to simply work but I get this error instead:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid type in JSON write (__SwiftValue)'
What am I doing wrong here?
Upvotes: 2
Views: 10737
Reputation: 285079
The JSON
type of SwiftyJSON
is a custom type and cannot be serialized.
Just get the dictionaryObject
from the JSON
object
if let params = self.myDictionary[self.selectedEntry].dictionaryObject {
And don't prettyPrint
. The server doesn't care
let json = try JSONSerialization.data(withJSONObject: params)
Note: You are encouraged to drop SwiftyJSON
in favor of Codable
.
Upvotes: 5