在尝试打印错误描述时,我正面临使用RestKit和Swift的对象映射问题。下面是我用于使用RestKit映射错误的代码:
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>
答案 0 :(得分:0)
我假设你想从RestKit NSError对象中获取自定义错误对象。我认为您可以使用以下方式实现此目的:
let customError = error.userInfo?[RKObjectMapperErrorObjectsKey] as? MyErrorModelClass
答案 1 :(得分:0)
如果您使用的是 RestKit v2.x ,则应以这种方式访问您的错误对象:
if let errorMessage = error.userInfo?[RKObjectMapperErrorObjectsKey]?.firstObject as? MyErrorModelClass{
//do your stuff
}
答案 2 :(得分:0)
我认为你只是错过了在alertView中显示它的调用。
var alert = UIAlertController(title: "Alert", message: error.localizedDescription, preferredStyle: UIAlertControllerStyle.Alert)