由于Apple决定将错误处理更改为更迂回的系统,因此我无法更新Xcode 7中的一行代码,并且遇到了一个我不熟悉的错误。
我收到错误:参数类型'AnyObject.Protocol'不符合预期类型'AnyObject'
这是我过去工作的旧代码:
request.HTTPBody = NSJSONSerialization.dataWithJSONObject(params, options: nil, error: &err)
我当前的代码抛出了上述错误:
do {
request.HTTPBody = try NSJSONSerialization.dataWithJSONObject(obj: AnyObject, options: nil)
} catch let error as NSError {
print("Could not be completed due to \(error)")
}
答案 0 :(得分:2)
请改为尝试:
do {
let request = try NSJSONSerialization.dataWithJSONObject(params, options: [])
} catch let error as NSError {
print("Could not be completed due to \(error)")
}