Lỗi Reference to generic type Dictionary requires arguments in <...>
xuất hiện trên dòng đầu tiên của hàm. Tôi đang cố gắng để có chức năng trả về một NSDictionary lấy từ một api. Có ai biết chuyện gì có thể xảy ra ở đây không?Lỗi Swift: Tham chiếu đến loại generic Từ điển yêu cầu đối số trong <...>
class func getCurrentWeather(longitude: Float, latitude: Float)->Dictionary?{
let baseURL = NSURL(string: "https://api.forecast.io/forecast/\(apikey)/")
let forecastURL = NSURL(string: "\(longitude),\(latitude)", relativeToURL:baseURL)
let sharedSession = NSURLSession.sharedSession()
let downloadTask: NSURLSessionDownloadTask = sharedSession.downloadTaskWithURL(forecastURL!, completionHandler: { (location: NSURL!, response: NSURLResponse!, error: NSError!) -> Void in
if(error == nil) {
println(location)
let dataObject = NSData(contentsOfURL:location!)
let weatherDictionary: NSDictionary = NSJSONSerialization.JSONObjectWithData(dataObject!, options: nil, error: nil) as NSDictionary
return weatherDictionary
}else{
println("error!")
return nil
}
})
}
EDIT:
Second Issue:
class func getCurrentWeather(longitude: Float, latitude: Float)->NSDictionary?{
let baseURL = NSURL(string: "https://api.forecast.io/forecast/\(apikey)/")
let forecastURL = NSURL(string: "\(longitude),\(latitude)", relativeToURL:baseURL)
let sharedSession = NSURLSession.sharedSession()
let downloadTask: NSURLSessionDownloadTask = sharedSession.downloadTaskWithURL(forecastURL!, completionHandler: { (location: NSURL!, response: NSURLResponse!, error: NSError!) -> Void in
if(error == nil) {
println(location)
let dataObject = NSData(contentsOfURL:location!)
let weatherDictionary: NSDictionary = NSJSONSerialization.JSONObjectWithData(dataObject!, options: nil, error: nil) as NSDictionary
return weatherDictionary //ERROR: NSDictionary not convertible to void
}else{
println("error!")
return nil ERROR: Type void does not conform to protocol 'NilLiteralConvertible'
}
})
}
Cảm ơn! Nó giải quyết lỗi đầu tiên nhưng bây giờ tôi nhận được một thông báo nói rằng 'NSDictionary' không thể chuyển đổi thành 'Void'. Bạn nghĩ gì đã xảy ra ở đây? –
@ user1851782: Bạn gặp lỗi ở đâu? Và bạn đang sử dụng mã nào? –
@ user1851782: Đó là vì 'let downloadTask: NSURLSessionDownloadTask = sharedSession.downloadTaskWithURL (forecastURL !, completionHandler: {(vị trí: NSURL !, response: NSURLResponse !, lỗi: NSError!) -> Void in' Kiểu của nó bị vô hiệu và nó là một cuộc gọi không đồng bộ –