我在嘗試打印錯誤描述時遇到了使用RestKit和Swift的對象映射問題。下面是我使用的地圖RestKit錯誤代碼:RESTkit + Swift的對象映射問題
let errorObjectMapping = RKObjectMapping(forClass: MyErrorModelClass.self)
errorObjectMapping.addAttributeMappingsFromArray([
"reason",
"errorMessage"
])
let errorCodes = NSMutableIndexSet()
errorCodes.addIndexes(RKStatusCodeIndexSetForClass(UInt(RKStatusCodeClassClientError)))
errorCodes.addIndexes(RKStatusCodeIndexSetForClass(UInt(RKStatusCodeClassServerError)))
objectManager.addResponseDescriptor(RKResponseDescriptor(
mapping: errorObjectMapping,
method: RKRequestMethod.Any,
pathPattern: nil,
keyPath: "error",
statusCodes: errorCodes))
代碼塊中,我打印的錯誤描述:錯誤的
failure: { (objectRequestOperation: RKObjectRequestOperation!, error: NSError!) -> Void in
println("error \(error).")
}
印刷描述:
Error Domain=org.restkit.RestKit.ErrorDomain Code=1004 <MyErrorModelClass: 0x17023bc00>" UserInfo=0x17406e480 {RKObjectMapperErrorObjectsKey=(
"<MyErrorModelClass: 0x17023bc00>
你想達到什麼目的? – Rengers 2015-03-31 14:19:52
在失敗塊中,我想顯示AlertView中的實際錯誤,但獲取對象類型響應。 當我更換 '讓errorObjectMapping = RKObjectMapping(forClass:MyCustomClassError.self)' 隨着 '讓errorObjectMapping:RKObjectMapping = RKObjectMapping.requestMapping()' 錯誤的印刷說明是: **錯誤域名= org.restkit.RestKit.ErrorDomain代碼= 1004 「」 的UserInfo = 0x170860040 {RKObjectMapperErrorObjectsKey =( { 原因= 「發生錯誤,請稍後再試。」; } ),NSLocalizedDescription = } ** –
2015-03-31 14:26:54
我沒有看到你的問題是,你還沒有完成編寫代碼。添加警報以使用錯誤 – Wain 2015-03-31 15:06:55