2015-01-01 10 views
9

numaralı argümanlara yönelik argümanlar gerektirir. Reference to generic type Dictionary requires arguments in <...> hatası, işlevin ilk satırında görüntülenir. Fonksiyonun bir api'den alınan bir NSDictionary döndürmesini sağlamaya çalışıyorum. Burada neler olabileceğini bilen var mı?Hızlı hata: Genel türden sözlüğe başvuru, <...>

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 

    } 
}) 
} 

DÜZENLEME:

İkinci Sayı:

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' 

     } 
    }) 
    } 

cevap

13

Bir Sözlük dönmek planlıyorsanız o zaman onun içinde anahtar ve veri türünü belirtmek gerekir.

Örn: Eğer birden çok veri türüne sahip İçinde veya eğer veriler hakkında emin değilseniz

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

, değişiklik: anahtar hem de değerin Strings o zaman böyle bir şey yazabilirsiniz ise Dictionary'dan NSDictionary'a dönüş tipi.

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

veya

Sen gibi yazabilirsiniz

:

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

teşekkür! İlk hatayı çözdü ancak şimdi 'NSDictionary' 'Void' 'e dönüştürülemeyen bir mesaj alıyorum. Sizce burada ne oldu? –

+0

@ user1851782: Hatayı nereden aldınız? Ve şimdi hangi kodu kullanıyorsunuz? –

+0

@ user1851782: Bunun nedeni, downloadTask: NSURLSessionDownloadTask = sharedSession.downloadTaskWithURL (tahminURL !, completionHandler: {: location: NSURL !, yanıt: NSURLResponse !, hata: NSError!) -> Void in 'tir zaman uyumsuz bir çağrı –

İlgili konular