В swift я использую AlamofireObjectMapper.Но я получаю сообщение об ошибке «ObjectMapper не удалось сериализовать ответ». Вот мой ответ API и модель, пожалуйста, дайте мне решение.В ответе не указывается нулевое значение.
Это ответ API
{"status":true,"result":[{"id":"246","sortname":"ZW","name":"Zimbabwe","phonecode":"263"},{"id":"245","sortname":"ZM","name":"Zambia","phonecode":"260"},{"id":"202","sortname":"ZA","name":"South Africa","phonecode":"27"},{"id":"244","sortname":"YU","name":"Yugoslavia","phonecode":"38"},{"id":"141","sortname":"YT","name":"Mayotte","phonecode":"269"},{"id":"243","sortname":"YE","name":"Yemen","phonecode":"967"},{"id":"91","sortname":"XU","name":"Guernsey and Alderney","phonecode":"44"}]}
Это вызов API
Alamofire.request(geturl, method: .get).responseArray { [weak self] (response: DataResponse<[countrymodel]>) in
print(response)
switch response.result {
case .success(let assignments):
let realm = try! Realm()
try! realm.write {
realm.delete(realm.objects(countrymodel.self))
realm.add(assignments, update: true)
}
for a in assignments{
print(a)
}
case .failure(let error):
print(".... . .. . .. . .")
}
}
Вот мой класс Model
import UIKit
import RealmSwift
import ObjectMapper
class countrymodel: Object, Mappable{
enum Status: Int, CustomStringConvertible {
case open, closed, cancelled
var description : String {
switch self {
case .open: return "Open"
case .closed: return "Closed"
case .cancelled: return "Cancelled"
}
}
}
@objc dynamic var id = 0
@objc dynamic var sortname = ""
@objc dynamic var name = ""
@objc dynamic var phonecode = ""
override static func primaryKey() -> String? {
return "id"
}
required convenience init?(map: Map) {
self.init()
}
func mapping(map: Map) {
id <- (map["id"])
sortname <- map["sortname"]
name <- map["name"]
phonecode <- map["phonecode"]
}
}