簡體   English   中英

無法將值類型“()”分配給字符串類型?

[英]Cannot assign value type '()' to type String?

我只是想從使用 completionHandler 的請求中獲取一些信息。 問題是我需要包含來自代碼其他部分的所有信息的數組,但似乎我無法訪問它。 這是我訪問數據的地方:

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)

在最后一行中,發生錯誤的地方是:“無法將 '()' 類型的值分配給 String 類型?我不確定我是否正確使用了 completionHandler 來檢索數據並確保我可以從不同的部分代碼。

有什么想法嗎? 非常感謝!

更新我沒有正確使用completionHandler。 所以我改變了它:

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

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

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

希望這有幫助!

由於這是異步網絡調用,您應該在 VC 的viewWillAppearviewDidAppearviewDidLoad啟動此調用——這取決於您是否需要重新加載數據。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM