json - 从 String 获取 Int 值

标签 json swift

我收到这样的 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/

相关文章:

json - 将新的键值对添加到Powershell中的JSON文件中。

python - 尝试在 YoutubeAnalytics Api 中使用 Pandas 解析 JSON 并将其转换为 CSV

iOS UIWebView loadHTMLString 不起作用

swift - 如果未在 init 中添加,则场景子项不会显示

ios - 无法转换类型 '_SwiftValue' 错误的值

JavaScript 将 JSON 数据提取到新对象中

ios - 快速从json获取位置

ios - 如何在Xcode中正确设置UIButton的约束(如图)?

swift - 按顺序显示 A、B、C、D 标签 : Swift

java - JSON、XML 或字符串连接