这就是我如何击中API-
APIService.shared.getSlots{ (success,weekSlots,error) in if success { self.weekSlots = weekSlots! print("success!!") } else { print(error?.errorMessage ?? "nopE") }}
而APIService类中的getSlot函数是 –
open func getSlots(completion: @escaPing (Bool,[WeekSlot]?,APIError?) -> ()) {sessionManager.request(APIRouter.getSlots()) .valIDate(statusCode: 200..<300) .responseArray(queue: nil,keyPath: "week_slots",context: nil) { (response: DataResponse<[WeekSlot]>) in switch response.result { case .success(let value): self.saveArraysToRealm(value: value) completion(true,value,nil) case .failure: let error = self.processFailure(Json: JsON(response.data as Any)) completion(false,nil,error) print(error) } }}
这是我的数据模型:
import Foundationimport ObjectMapperimport RealmSwiftclass WeekSlot: Object,Mappable { dynamic var date : String? = "" var slot = List<Slots>() //Impl. of Mappable protocol required convenIEnce init?(map: Map) { self.init() } func mapPing(map: Map) { date <- map["date"] slot <- (map["slots"],Arraytransform<Slots>()) }}
我已经声明了执行get请求的请求,并且url也是正确的.除了由sessionManager处理的auth令牌之外,API不接受任何参数.但是,我在调试时得到以下错误响应 –
[Request]: GET http://beta.xamIDea.in/API/v1/teachers/get_slots/[Response]: <NShttpURLResponse: 0x600000434c80> { URL: http://beta.xamIDea.in/API/v1/teachers/get_slots/ } { status code: 200,headers {Allow = "GET,POST,head,OPTIONS";Connection = "keep-alive";"Content-Length" = 477;"Content-Type" = "application/Json";Date = "Tue,10 Oct 2017 11:01:53 GMT";Server = "Nginx/1.10.3 (Ubuntu)";vary = Accept;"x-frame-options" = SAMEORIGIN;} }[Data]: 477 bytes[Result]: FAILURE: Error Domain=com.alamofireobjectmapper.error Code=2 "ObjectMapper Failed to serialize response." UserInfo=. {NSLocalizedFailureReason=ObjectMapper Failed to serialize response.}[Timeline]: Timeline: { "Request Start Time": 529326113.851,"Initial Response Time": 529326113.985,"Request Completed Time": 529326113.986,"Serialization Completed Time": 529326113.987,"Latency": 0.134 secs,"Request Duration": 0.135 secs,"Serialization Duration": 0.001 secs,"Total Duration": 0.136 secs } ▿ request : Optional<URLRequest>▿ some : http://beta.xamIDea.in/API/v1/teachers/get_slots/ ▿ url : Optional<URL> ▿ some : http://beta.xamIDea.in/API/v1/teachers/get_slots/ - cachePolicy : 0 - timeoutInterval : 60.0 - maindocumentURL : nil - networkServiceType : __ObjC.NSURLRequest.NetworkServiceType - allowsCellularaccess : true ▿ httpMethod : Optional<String> - some : "GET" ▿ allhttpheaderFIElds : Optional<Dictionary<String,String>> ▿ some : 1 element ▿ 0 : 2 elements - key : "Authorization" - value : "Token 4d7ebe501bcd7c910cf1950ab53bc8aa2a4a569d" - httpBody : nil - httpBodyStream : nil - httpShouldHandlecookies : true - httpShouldUsePipelining : false ▿ response : Optional<NShttpURLResponse> ▿ data : Optional<Data>▿ some : 477 bytes - count : 477 ▿ pointer : 0x00007f896a48aa80 - pointerValue : 140228170394240 ▿ result : FAILURE: Error Domain=com.alamofireobjectmapper.error Code=2 "ObjectMapper Failed to serialize response." UserInfo= {NSLocalizedFailureReason=ObjectMapper Failed to serialize response.} ▿ timeline : Timeline: { "Request Start Time": 529326113.851,"Initial Response Time": 529326113.985,"Total Duration": 0.136 secs }- requestStartTime : 529326113.85062999- initialResponseTime : 529326113.98505801- requestCompletedTime : 529326113.98612601- serializationCompletedTime : 529326113.986781- latency : 0.13442802429199219- requestDuration : 0.13549602031707764- serializationDuration : 0.00065499544143676758- totalDuration : 0.1361510157585144 ▿ _metrics : Optional<AnyObject>
这个错误意味着什么?
API对成功的回应是 –
{ "result": { "week_slots": [ { "date": "2017-10-10","slots": [] },{ "date": "2017-10-11","slots": [ { "start": "2017-10-11T20:00:00Z","end": "2017-10-11T21:00:00Z","availability": true,"booked": false },{ "start": "2017-10-11T10:00:00Z","end": "2017-10-11T12:00:00Z","booked": false } ] },{ "date": "2017-10-12",{ "date": "2017-10-13",{ "date": "2017-10-14",{ "date": "2017-10-15",{ "date": "2017-10-16","slots": [] } ]},"success": true,"error": {}}解决方法 尝试将模型类更改为:
class WeekSlot: Object,Mappable { dynamic var date : String? = "" var slot: [Slots] = [] //Impl. of Mappable protocol required convenIEnce init?(map: Map) { self.init() } func mapPing(map: Map) { date <- map["date"] slot <- map["slots"] }}
还要检查你的插槽型号
总结以上是内存溢出为你收集整理的ios – ObjectMapper无法序列化响应错误Code = 2全部内容,希望文章能够帮你解决ios – ObjectMapper无法序列化响应错误Code = 2所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)