Reputation: 1
I am getting this error, and after researching I still have no idea how to solve it.
Maybe it is something about swift 3 because it appeared when I updated it.
The exact error is in:
guard let task = URLSession.shared.dataTask(with: request, completionHandler: {(data:Data?, response:URLResponse?, error:NSError?) -> Void in
I would appreciate any help.
class func faceForPerson (_ person:String, size:CGSize, completion:@escaping (_ image:UIImage?, _ imageFound:Bool?)->()) throws {
let escapedString = person.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlHostAllowed)
let pixelsForAPIRequest = Int(max(size.width, size.height)) * 2
let url = URL(string: "https://en.wikipedia.org/w/api.php?action=query&titles=\(escapedString!)&prop=pageimages&format=json&pithumbsize=\(pixelsForAPIRequest)")
var request = URLRequest(url:url! as URL);
request.httpMethod = "GET";
guard let task = URLSession.shared.dataTask(with: request, completionHandler: {(data:Data?, response:URLResponse?, error:NSError?) -> Void in
if error == nil {
let wikiDict = try! JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.allowFragments) as! NSDictionary
if let query = wikiDict.object(forKey: "query") as? NSDictionary {
if let pages = query.object(forKey: "pages") as? NSDictionary {
if let pageContent = pages.allValues.first as? NSDictionary {
if let thumbnail = pageContent.object(forKey: "thumbnail") as? NSDictionary {
if let thumbURL = thumbnail.object(forKey: "source") as? String {
let faceImage = UIImage(data: try! Data(contentsOf: URL(string: thumbURL)!))
completion(faceImage, true)
}
}else{
completion(nil, false)
}
}
}
}
}
} as! (Data?, URLResponse?, Error?) -> Void)else{
throw WikiFaceError.couldNotDownloadImage
}
task!.resume()
}
Upvotes: 0
Views: 438
Reputation: 9943
This guard let task = URLSession.shared.dataTask(with: request, completionHandler: {(data:Data?, response:URLResponse?, error:NSError?) -> Void in
doesn't create optional data task (it wont be nil), so you don't need to check optional with guard let
, just use let
Also makes no sense you tried to force it become (Data?, URLResponse?, Error?) -> Void)
?
Upvotes: 0