2017-07-25 127 views
-4

我只是想從使用completionHandler的請求中獲取一些信息。問題是我需要使用來自代碼其他部分的所有信息的數組,並且看起來我無法訪問它。這是我訪問數據:不能賦值類型'()'來鍵入String?

private func loadUserData(completionHandler: @escaping ([String]) ->()) { 
    // We're getting user info data from JSON response 
    let session = Twitter.sharedInstance().sessionStore.session() 
    let client = TWTRAPIClient.withCurrentUser() 
    let userInfoURL = "https://api.twitter.com/1.1/users/show.json" 
    let params = ["user_id": session?.userID] 
    var clientError : NSError? 
    let request = client.urlRequest(withMethod: "GET", url: userInfoURL, parameters: params, error: &clientError) 
    client.sendTwitterRequest(request) { (response, data, connectionError) -> Void in 
     if connectionError != nil { 
      print("Error: \(connectionError)") 
     } 

     do { 
      let json = JSON(data: data!) 
      if let userName = json["name"].string, 
       let description = json["description"].string, 
       let followersCount = json["followers_count"].int, 
       let favouritesCount = json["favourites_count"].int, 
       let followingCount = json["friends_count"].int, 
       let lang = json["lang"].string, 
       let nickname = json["screen_name"].string { 

        self.userData.append(userName) 
        self.userData.append(description) 
        self.userData.append(String(followersCount)) 
        self.userData.append(String(favouritesCount)) 
        self.userData.append(String(followingCount)) 
        self.userData.append(lang) 
        self.userData.append(nickname) 

        completionHandler(self.userData) 
      } 
     } 
    } 
} 

func manageUserData(index: Int) { 
    loadUserData() {_ in 
     return self.userData[index] 
    } 
} 

這裏,它是我需要這些數據來顯示它:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let rowNumber = indexPath.row 
    let cellIdentifier = "TableViewCell" 
    guard let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as? TableViewCellController else { 
     fatalError("The dequeued cell is not an instance of TableViewCellController.") 
    } 

    switch rowNumber { 
     case 0: 
      cell.titlePlaceholder.text = "Name:" 
      cell.valuePlaceholder.text = manageUserData(index: 1) 

在最後一行是當錯誤發生時:「不能ASIGN類型'()'的值爲String類型。我不確定我是否正確使用completionHandler以檢索數據並確保我可以從代碼的不同部分訪問它。非常感謝!

UPDATE 我沒有正確使用completionHandler。

private func loadUserData(completion: @escaping (_ myArray: [String]) -> Void) 

所以我manageUserData函數現在看起來是這樣的:

func manageUserData() { 
    loadUserData { 
     (result: [String]) in 
     self.secondUserData = result 
    } 
} 

希望這有助於所以我通過改變它!

+0

不清楚你期望的。你的'func manageUserData(index:Int)'不會返回任何值。因此,您不能將其分配給標籤的「文本」。它不是文字。它不是一個字符串。這是一個無效(沒有價值)。 – matt

回答

1

由於這是異步網絡調用,您應該在您的VC的viewWillAppear,viewDidAppearviewDidLoad開始此調用 - 取決於您是否需要重新加載數據。

override func viewWillAppear(_ animate: Bool) { 
    super.viewWillAppear(animate) 
    sessionManager.loadUserData { (strings) in 

    } 
} 
loadUserData封將要運行時調用完成後,將數據加載到作爲的tableView的數據源的數組,並調用 tableView.reloadData()

sessionManager.loadUserData { (strings) in 
    self.dataSource = strings 
    self.tableView.reloadData() 
} 

然後你cellForRow你會內部

然後只需要:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let rowNumber = indexPath.row 
    let cellIdentifier = "TableViewCell" 
    guard let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as? TableViewCellController else { 
     fatalError("The dequeued cell is not an instance of TableViewCellController.") 
    } 

    switch rowNumber { 
     case 0: 
      cell.titlePlaceholder.text = "Name:" 
      cell.valuePlaceholder.text = dataSource[0] 
    } 
} 

確保您numberOfRows將返回dataSource.count

相關問題