2017-01-02 78 views
-1

我是新來的斯威夫特3,我有下面的代碼有問題:未解決的標識符範圍

func downloadData() {... downloads data about actors in JSON and initializes a Struct Actor} 

則:

class MasterViewController: UITableViewController { 

    var dataArray = [Actor]() //declare an empty array of type Actor 

    override func viewDidLoad() { 

     super.viewDidLoad() 

     dataArray = downloadData() //to populate an empty array with data downloaded from web 

     print(dataArray) 

當我嘗試編譯我收到的錯誤未解決的代碼標識符。我認爲這是因爲範圍。任何人都可以指出我的正確方向嗎?謝謝!

+1

我們需要更多信息繼續下去。什麼標識符尚未解決?錯誤發生在哪裏? –

+1

你在哪裏定義'downloadData()'?它返回什麼?它是從Web下載數據的異步函數嗎?如果是的話,你需要回電或代表或類似的。 –

+0

爲什麼要創建一個新的數組,將其分配給'dataArray',然後用'downloadData()'的結果立即覆蓋它? – Alexander

回答

0
class MasterViewController: UITableViewController { 

    var dataArray = [Actor]() //declare an empty array of type Actor 

    override func viewDidLoad() { 

     super.viewDidLoad() 

     dataArray = downloadData() //to populate an empty array with data downloaded from web 

     print(dataArray) 
    } 

    func downloadData() -> [Actor] { 

     return [Actor]() 
    } 
} 

只需實施downloadData方法。 編輯: 您可以使用EVReflection和Alamofire(Pods)來下載json並創建Actor並填充數組。

+0

當我嘗試填充一個空數組'dataArray = downloadData()'我收到此錯誤:無法指定type()的值來鍵入[Actor]。 – Luke

+1

你確定在你的代碼中有「func downloadData() - > [Actor]」嗎? – Axel

1

您的功能/方法,downloadData()不返回任何內容。

相關問題