我只是想使用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
}
}
希望这有帮助!
答案 0 :(得分:1)
由于这是异步网络通话,您应该在VC viewWillAppear
,viewDidAppear
或viewDidLoad
中启动此通话,具体取决于您是否需要数据重装。
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