【发布时间】:2018-02-21 18:29:14
【问题描述】:
您好,我正在尝试提取 json。但是我收到一条错误消息Cannot convert value of type 'Data' to expected argument type 'NSData'。有什么我做错了吗?
success: { (response) -> Void in
var dataStream: Data = Data.init(referencing: response!)
do {
let data = try JSONSerialization.jsonObject(with: dataStream, options: JSONSerialization.ReadingOptions.mutableContainers)as AnyObject
}catch{
}
if let id = response!["money"]as? String {
print(id)
}
}){ (error) -> Void in
print("error")
}
}
更新2
let request = Alamofire.request(apiUrl, method: .get)
request.responseJSON { response in
switch response.result
{
case .success:
success(response.result.value as AnyObject)
case .failure(let error):
failure(error as NSError)
}
}
}
【问题讨论】:
-
我删除了我的答案,因为错误消息似乎具有误导性。初始化器
init(referencing无论如何都是不合适的,因为它从值类型中创建了一个引用类型,这在您的情况下是无用的。我推荐使用 Alamofire 的.responseJSON参数返回反序列化的 JSON。 -
我实际上已经实现了更新 2
-
试图打印 response.result.value???