2016-01-06 37 views
-1

我想調用另一個類中的函數。但他們並沒有迅速返回字典。如何解決這個問題?在Swift中返回值

在我ViewController

var dict = NSDictionary() 
dict = temp.GetStation(myUrl) 

在我的課堂

func GetStation(url : String) -> NSDictionary { 

    let dicts = NSDictionary() 
    getResonse(url, completionhandler: { (dict) -> NSDictionary in 
    // print(dict) 

     return dict      
    }) 

    return dicts; 
} 

回答

0

試試這個代碼。

func GetStation(url : String, completionHandler: (stationDictionary: NSDictionary) -> NSDictionary) { 

     getResonse(url, completionhandler: { (dict) -> NSDictionary in 
      // print(dict) 

      completionHandler(stationDictionary: dict) 
     }) 

    } 

,並使用它像,

var dict = NSDictionary() 
temp.GetStation("your url") { (stationDictionary) -> NSDictionary in 
      dict = stationDictionary; 
      print("your dictionary := \(stationDictionary)") 
     } 

我們增加completionHandler代替return value,因爲在功能GetStation您調用函數getResonse這臺異步,可調用web服務。

因此GetStation的響應取決於getResonse的響應。

+0

completionHandler(stationDictionary:dict)在這一行出現錯誤無法將type()的值轉換爲封閉類型Nsdictionary –

+0

請檢查編輯的答案,謝謝 –

+0

工作是否正常? –

相關問題