2015-01-01 17 views
9

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' 

     } 
    }) 
    } 

Trả lời

13

Nếu bạn đang có kế hoạch quay trở lại một từ điển thì bạn cần phải xác định loại quan trọng và dữ liệu trong đó.

Ví dụ: Nếu khoá và giá trị của bạn cả hai đều là Strings sau đó bạn có thể viết một cái gì đó như:

class func getCurrentWeather(longitude: Float, latitude: Float) -> Dictionary <String, String>? 
{ 
    ... 
} 

Nếu bạn không chắc chắn về dữ liệu trong nó, hoặc nếu bạn có nhiều loại dữ liệu, thay đổi loại trả về từ Dictionary đến NSDictionary.

class func getCurrentWeather(longitude: Float, latitude: Float) -> NSDictionary? 
{ 
    ... 
} 

hay

Bạn có thể viết như sau:

class func getCurrentWeather(longitude: Float, latitude: Float) -> Dictionary <String, AnyObject>? 
{ 
    ... 
} 
+0

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? –

+0

@ user1851782: Bạn gặp lỗi ở đâu? Và bạn đang sử dụng mã nào? –

+0

@ 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ộ –

Các vấn đề liên quan