gpt4 book ai didi

json - 对成员 'dataTask(with:completionHandler:)' 错误的引用不明确

转载 作者:行者123 更新时间:2023-11-30 12:04:07 24 4
gpt4 key购买 nike

我收到此错误:

Ambiguous reference to member 'dataTask(with:completionHandler:)'

查看代码发生的位置(添加错误行下方的注释)

{
for metadata in metadataObjects{
let decodedData:AVMetadataMachineReadableCodeObject = metadata as! AVMetadataMachineReadableCodeObject

if currentISBN == decodedData.stringValue { continue; }

currentISBN = decodedData.stringValue

//self.lblDataInfo.text = decodedData.stringValue
//self.lblDataType.text = decodedData.type

let url = URL(string: "https://www.googleapis.com/books/v1/volumes?q=isbn:"+decodedData.stringValue+"&key=AIzaSyAf9fhYZLHjjqfWiTXZeSikNTZOt5yNwoU")
let session = URLSession.shared
let request = NSMutableURLRequest(url: url!)
request.httpMethod = "GET"
request.addValue(Bundle.main.bundleIdentifier ?? "", forHTTPHeaderField: "X-Ios-Bundle-Identifier")

//HERE I GET THE ERROR ON THIS LINE:
**let task = session.dataTask(with: request, completionHandler: { (data, response, error) -> Void in**

if error != nil {
print(error)
return
}

do {
//let jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) as! NSDictionary

let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers)

if json is [String: AnyObject] {
print(json)
if let error = json["error"] as? String {
print(error);
} else if let items = json["items"] as? [[String: AnyObject]] {

for item in items {
print(item)

let book_id = item["id"] as? String

if let volumeInfo = item["volumeInfo"] as? [String: AnyObject] {
let book_title = volumeInfo["title"] as? String
DispatchQueue.main.async(execute: { () -> Void in
self.lblDataInfo.text = "ISBN: "+self.currentISBN!+" ID:"+book_id!
self.lblDataType.text = book_title
})
}

break // for now, only show first
}

} else {
DispatchQueue.main.async(execute: { () -> Void in
self.lblDataInfo.text = "ISBN: "+self.currentISBN!+" Not identified"
self.lblDataType.text = ""
})
}
}

} catch let jsonError {
print(jsonError)
}

})
task.resume()

}
}

最佳答案

该方法需要 native URLRequest,而不是 NSMutableURLRequest

var request = URLRequest(url: url!)

Swift 3+ 中的 JSON 字典是 [String:Any] 而不是 [String:AnyObject]

关于json - 对成员 'dataTask(with:completionHandler:)' 错误的引用不明确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46863006/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com