Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift ObjectMapper未能序列化响应。使用AlamoFileObject映射器从API响应_Swift_Model_Response - Fatal编程技术网

Swift ObjectMapper未能序列化响应。使用AlamoFileObject映射器从API响应

Swift ObjectMapper未能序列化响应。使用AlamoFileObject映射器从API响应,swift,model,response,Swift,Model,Response,在swift中,我使用的是AlamofireObject Mapper。但我收到错误“ObjectMapper未能序列化响应。”这是我的api响应和模型,请给我解决方案。作为响应,没有任何空值 这是API响应 {"status":true,"result":[{"id":"246","sortname":"ZW","name":"Zimbabwe","phonecode":"263"},{"id":"245","sortname":"ZM","name":"Zambia","phonecode"

在swift中,我使用的是AlamofireObject Mapper。但我收到错误“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(".... . .. . .. . .")
        }

    }
Alamofire.request(geturl,方法:.get).responseArray{[weak self](response:DataResponse)位于
打印(答复)
开关响应。结果{
案例。成功(让作业完成):
让realm=try!realm()
试试看!写吧{
realm.delete(realm.objects(countrymodel.self))
realm.add(分配,更新:true)
}
对于作业中的{
印刷品(a)
}
案例。失败(let错误):
打印(“…………”)
}
}
这是我的模型课

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"]
    }
}
导入UIKit
导入RealmSwift
导入对象映射器
类countrymodel:对象,可映射{
枚举状态:Int,CustomStringConvertible{
箱子打开、关闭、取消
变量说明:字符串{
切换自身{
case.open:返回“open”
案例.关闭:返回“关闭”
案例.已取消:返回“已取消”
}
}
}
@objc动态变量id=0
@objc动态变量sortname=“”
@objc动态变量名称=“”
@objc动态变量phonecode=“”
重写静态func primaryKey()->字符串{
返回“id”
}
所需便利初始化?(映射:映射){
self.init()
}
func映射(映射:映射){

id您的响应不是一个数组,而是一个字典,其中有一个名为
result
的数组,因此序列化失败,您可能会使它失败

Alamofire.request(geturl, method: .get).responseJSON
并仅解析该数组

您可以尝试以下代码: 更改此代码

params).responseArray(completionHandler: { (response: DataResponse<[countrymodel]>) in
params).responseArray(completionHandler:{(response:DataResponse)在
像这样:

params).responseObject {(response: DataResponse<countrymodel>) in
params).responseObject{(响应:DataResponse)在