You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Swift:如何将Codable的User数组转换为姓名字符串数组?

将[User]数组转换为[String]数组适配可折叠TableView

假设你的User模型定义如下:

struct User: Codable {
    let user_id: String // 若实际类型为Int可自行调整
    let name: String
}

当你已有一个[User]类型的数组(比如命名为users),只需用Swift的map方法就能快速提取所有用户的name属性,生成符合教程要求的[String]数组:

let namesArray: [String] = users.map { $0.name }

得到的namesArray即可直接用于构建教程中的ExpandableNames数组。

内容的提问来源于stack exchange,提问作者mystic cola

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 23:32:02