I'm pretty sure that my problem is easy solving, but I can't find any solution. So I have Alamofire request and have a trouble with handling data types. I have so many 'printing out' just to check what data I've got step by step.
Alamofire.request(URL, method: .get, headers: headers).responseJSON { response in
switch responseJSON.result {
case .success(let value):
print(type(of: value)) //__NSDictionaryI
print(value)
print(type(of:responseJSON)) //DataResponse<Any>
print(responseJSON) . //SUCCESS: {"billing_addresses" = (...
print(responseJSON.value as Any) . //Optional({...
//print(responseJSON.value as! [[String:Any]]) . //Could not cast value of type '__NSDictionaryI' (0x10b9fb508) to 'NSArray' (0x10b9fb008).
do {
let decoder = JSONDecoder()
let model = try decoder.decode(Info.self, from: value as! Data) //Decode JSON Response Data
print(model.id)
} catch let parsingError {
print("Error", parsingError)
}
Now I have an error: **Could not cast value of type '__NSSingleEntryDictionaryI' (0x10d240f78) to 'NSData' (0x10d241090).**
value of responseJSON is:
(I'm not sure that this value is correct, because when I've check in Postman all strings is doublequoted, and value of "is_default" is true/false, not 0/1. But in the Xcode I've got this in the console. So maybe problem in the responseJSON?..)
And there could be zero address, or several ones.
{
"id": 40128,
"username": "test6",
"email": "test6@on.com",
"billing_addresses": [
{
"address_name": null,
"country_code": "US",
"first_name": "Ted",
"last_name": "Qqqq",
"company_name": "",
"address_line1": "308 Sea Lane",
"address_line2": "",
"city": "QQQQ",
"state": "FL",
"postcode": "32000",
"email_address": "test6@on.com",
"phone_number": "11111111",
"is_default_for_billing": true
}
],
"shipping_addresses": [
{
"address_name": null,
"country_code": "US",
"first_name": "Ted",
"last_name": "Qqqq",
"company_name": "",
"address_line1": "308 Sea Lane",
"address_line2": "",
"city": "QQQQ",
"state": "FL",
"postcode": "32000",
"is_default_for_shipping": true
}
]
}
And here is model
struct Info : Decodable {
let id: Int
let email: String
let username: String
let billing_addresses: Billings
let shipping_addresses: Shippings
}
struct Billings: Decodable{
let address_name: String
let country_code: String
let first_name: String
let last_name: String
let company_name: String
let address_line1: String
let address_line2: String
let city: String
let state: String
let postcode: String
let email_address: String
let phone_number: String
let is_default_for_billing: Bool
}
struct Shippings:Decodable{
let address_name: String
let country_code: String
let first_name: String
let last_name: String
let company_name: String
let address_line1: String
let address_line2: String
let city: String
let state: String
let postcode: String
let is_default_for_shipping: Bool
}
If I try to use SwiftyJSON with value
as parameter I have an error that Any
couldn't be Data
and I really don't know what should I do.