我试图承诺一个json对象。
但是,jsonserialization.jsonobject不接受我的任何参数。
class func deserialize<T>(data: Data) -> Promise<T> {
let deserialized = try! JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.allowFragments)
String(data: data, encoding: String.Encoding.utf8)
// Deserialize or try as String
guard let object = deserialized else {
return Promise<T>(error: APIError.InvalidJSON(data) as Error)
}
// Cast as T
guard let objectT = object as? T else {
return Promise<T>(error: APIError.JSONTypeMismatch(object, T.self))
}
return Promise(objectT)
}
我收到两个错误:
let desserialized = try! JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.allowFragments)
不能用类型为(with:data,options:jsonserialization.readingoptions)的参数列表调用“jsonobject”
String(data: data, encoding: String.Encoding.utf8)
“无法将'data'类型的值转换为所需的参数类型'data'”