在swift中转换对象时出现类型不匹配错误
如何在转换city_id中的对象时将字符串转换为Int 从api接收数据并转换为对象时出错 如何在转换city_id中的对象时将字符串转换为Int 转换对象的代码在swift中转换对象时出现类型不匹配错误,swift,Swift,如何在转换city_id中的对象时将字符串转换为Int 从api接收数据并转换为对象时出错 如何在转换city_id中的对象时将字符串转换为Int 转换对象的代码 编辑:是的,您不应该在生产代码中强制展开值-前面的代码只是为了演示。无论如何,我编辑了关于上面@Joakim Danielson评论的代码 您应该实现一个自定义initfrom decoder:decoder throws方法 实现您自己的initfrom decoder:decoder或将属性声明为String@JoakimDani
编辑:是的,您不应该在生产代码中强制展开值-前面的代码只是为了演示。无论如何,我编辑了关于上面@Joakim Danielson评论的代码 您应该实现一个自定义initfrom decoder:decoder throws方法
实现您自己的initfrom decoder:decoder或将属性声明为String@JoakimDanielson谢谢你的评论,你有任何例子吗?下面的答案是一个例子,但跳过可怕的!试过之后。请注意,如果按此方法执行,则需要处理每个属性,因此更简单的解决方案是在结构中将属性声明为字符串。有没有办法检查cityID是string还是Int?我编辑了代码以处理optionals并更优雅地失败。
typeMismatch(Swift.Int, Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "city_id", intValue: nil)], debugDescription: "Expected to decode Int but found a string/data instead.", underlyingError: nil))
class ObjUserInfo: Codable {
var id: Int
var firstName, lastName, email: String
var cityID: Int
var city, country: String
var countryID: Int
var countryFlag: String
var phone, birthDate: String
var gender: Int
var genderText: String
var profileImage, coverImage: String
var profileInfo: String
var accessInfo: AccessInfo
var userFavouriteSports: [ObjSportsList]
var languages: String
enum CodingKeys: String, CodingKey {
case id
case firstName = "first_name"
case lastName = "last_name"
case email
case cityID = "city_id"
case city, country
case countryID = "country_id"
case countryFlag = "country_flag"
case phone
case birthDate = "birth_date"
case gender
case genderText = "gender_text"
case profileImage = "profile_image"
case coverImage = "cover_image"
case profileInfo = "profile_info"
case accessInfo = "access_info"
case userFavouriteSports = "user_favourite_sports"
case languages
}
init(id: Int, firstName: String, lastName: String, email: String, cityID: Int, city: String, country: String, countryID: Int, countryFlag: String, phone: String, birthDate: String, gender: Int, genderText: String, profileImage: String, coverImage: String, profileInfo: String, accessInfo: AccessInfo, userFavouriteSports: [ObjSportsList], languages: String) {
self.id = id
self.firstName = firstName
self.lastName = lastName
self.email = email
self.cityID = cityID
self.city = city
self.country = country
self.countryID = countryID
self.countryFlag = countryFlag
self.phone = phone
self.birthDate = birthDate
self.gender = gender
self.genderText = genderText
self.profileImage = profileImage
self.coverImage = coverImage
self.profileInfo = profileInfo
self.accessInfo = accessInfo
self.userFavouriteSports = userFavouriteSports
self.languages = languages
}
}
let decoderdec = JSONDecoder()
//decoderdec.keyDecodingStrategy = .convertFromSnakeCase
// 2. Create Data from Response
let jsonData = try JSONSerialization.data(withJSONObject: jsonResponse["data"] as Any)
let objUser = try decoderdec.decode(ObjUserInfo.self, from: jsonData)
init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
// Left hand side `id` is for the property, and `forKey: .id` is the case in
// your enum CodingKeys
id = try values.decode(Int.self, forKey: .id)
...
// First try decoding from String, and covert it to Int.
// If it wasn't String, try decoding from Int.
if let rawCityID = try? values.decode(String.self, forKey: .cityID),
let cityID = Int(rawCityID) {
self.cityID = cityID
} else {
cityID = try values.decode(Int.self, forKey: .cityID)
}
...
}