Why does the following code give me the error:
Invalid type in JSON write (_SwiftValue).
The error is thrown on this line:
Had the same problem and error as you! FINALLY found the issue...
My code:
params = [
"gender": request.gender.first ?? "",
"age": 15
]
Problem: Even though request.gender.first ?? "" returns a string, its a type of String.Element, which ANY JSONEncoder or JSONSerialization cannot encode (and is not in the list of types it can handle, according to documentation).
Solution:
params = [
"gender": request.gender.first?.description ?? "",
"age": 15
]
Typically, just make sure its a string or an appropriate number the Encoders can handle...