我收到这样的 api 响应:
{"id":10,"user_name":"test"}
我能够毫无问题地解析 TableView 中的用户名,但 ID 始终显示为空白
这是我的 cellForRowAt 函数:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "userDataCell", for: indexPath) as! UserDataCell
let userDetails = responseArray[indexPath.row]
cell.userNameField.text = userDetails["user_name"] as? String
cell.idTextField.text = userDetails["id"] as? String
return cell
}
我猜是我在获取 Int 时尝试解析字符串的问题
将 修改为?整数
cell.idTextField.text = userDetails["id"] as? Int
没有解决问题。有帮助吗?
最佳答案
你能试试这个作为临时解决方案吗?
cell.idTextField.text = String(userDetails["id"] as? Int ?? 0)
关于json - 从 String 获取 Int 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55024294/