惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

F
Full Disclosure
V
Vulnerabilities – Threatpost
Attack and Defense Labs
Attack and Defense Labs
N
News and Events Feed by Topic
SecWiki News
SecWiki News
S
Security @ Cisco Blogs
Schneier on Security
Schneier on Security
B
Blog
TaoSecurity Blog
TaoSecurity Blog
The Last Watchdog
The Last Watchdog
H
Hacker News: Front Page
Hacker News - Newest:
Hacker News - Newest: "LLM"
博客园_首页
D
Docker
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Y
Y Combinator Blog
W
WeLiveSecurity
N
News and Events Feed by Topic
F
Fortinet All Blogs
PCI Perspectives
PCI Perspectives
WordPress大学
WordPress大学
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Recent Announcements
Recent Announcements
Forbes - Security
Forbes - Security
T
Tailwind CSS Blog
Hacker News: Ask HN
Hacker News: Ask HN
爱范儿
爱范儿
腾讯CDC
Last Week in AI
Last Week in AI
月光博客
月光博客
C
Cybersecurity and Infrastructure Security Agency CISA
P
Proofpoint News Feed
Help Net Security
Help Net Security
V
V2EX
C
Cyber Attacks, Cyber Crime and Cyber Security
C
CXSECURITY Database RSS Feed - CXSecurity.com
H
Heimdal Security Blog
L
LINUX DO - 最新话题
GbyAI
GbyAI
The Hacker News
The Hacker News
罗磊的独立博客
S
SegmentFault 最新的问题
H
Hackread – Cybersecurity News, Data Breaches, AI and More
博客园 - 【当耐特】
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
V2EX - 技术
V2EX - 技术
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
O
OpenAI News
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻

Swift

都 vibe coding 了,为啥不把 Rust 项目 rewrite 到 Swift ? 应该怎么解读 swiftui 的崩溃报告? FoundationModels 框架实践 - V2EX Swift DocC 构建的博客 - V2EX Swift server 要支棱起来了 - V2EX 求问贴,如果在 swiftUI 中接入 MySQL C client - V2EX Swift 萌新, DispatchQueue 的一个问题 - V2EX 5 分钟用 Swift 写个 ripripgrep - V2EX Swift 操作 plist 文件 使用 SwiftUI + TCA 重构 Instagram Clone:从 Flutter 到 SwiftUI 的迁移实践 激发编程激情,轻松掌握 Swift SwiftUI 分享个基于 UICollectionView 封装的数据驱动和声明式组件库 ListKit 问一个关于 CALayer 的资源释放问题 SwiftUI 的 TextEditor 在 macOS Sequoia 15.0 上输入中文时又会这样一跳一跳的了 Swift 中系统 API delegate 参数一定要在当前类实现传 self 吗 ScreenCaptureKit 里面的 SCStreamOutput 没有被顺利触发,是那个步骤错了,求解惑 - V2EX 似乎 SwiftUI app 在 macOS 15 上启动速度变快很多? - V2EX 请教一个关于 Swift 的基础小问题 - V2EX 离线 iOS app 也需要 ICP ? - V2EX 请教 SwiftUI 布局的问题 - V2EX ios 17 widget 无法正确更新 core data - V2EX 有没有 Swift ui 版本的 tailwind css? - V2EX swiftui 里,这里用 hstack,为什么是垂直布局,而不是水平布局? - V2EX 大家觉得 Swift 的参数必须按序提供怎么样? - V2EX Swift 开源的组件库好像不是很多啊 - V2EX Swift 如何像 Python 那样 崩溃了输出 backtrace 或者至少是哪行出错了。。。 - V2EX 请教一下各位 iOSer 巨佬们一个 SwiftUI sheet 组件的问题 - V2EX 请教一下各位 iOSer 巨佬们一个 SwiftUI 中 HStack 的布局问题 - V2EX swiftui 有好用的组件库吗? - V2EX 求问:苹果手机是否无法像安卓一样,可以扫描出本地的音乐? - V2EX macOS 沙盒中如何获得对任意目录的写权限? - V2EX swiftui 中的语法糖疑问 - V2EX 开发 macOS 应用无法访问用户选择的文件夹下的文件 - V2EX 弄了一个 Swift 官方教程的速览版 - V2EX 2024 年了,现在学 SwiftUI 还有必要学 UIkit 吗? - V2EX 大佬们指点下苹果生态下音频相关的这些库都是什么关系? - V2EX 升级到 Xcode 15 之后遇到的 GeneratedAssetSymbols 过不了编译这个问题 - V2EX 现在用 Swift 开发后端成熟了吗? - V2EX Composable SwiftUI - V2EX 2023 年, swiftui 使用 WKWebView 加载 html 字符串,如何自适应 frame 高度? - V2EX 用 network extension 框架开启 Tunnel,怎么能保持后台请求接口,解决请喝杯咖啡 - V2EX 学生党求问各位大佬,一个关于 ios 小组件的问题 - V2EX 比较好的 Swift UI 学习交流群 - V2EX CAID 这垃圾东西是谁在推,一边喊着保护隐私,一边想着法子来跟踪用户 - V2EX Swift 的 Optional 如何比较包装类型的关系呢? - V2EX Alamofire 推荐使用吗? - V2EX 我自己是初学者想找个朋友带我一起写两个工作用的 app 有偿的 - V2EX Swift 如何优化数据加载方式? - V2EX 现在感觉 Swift 真的被 OC 拖后腿了 - V2EX
分享一下一个 Web 前端刚学习 Swift 时候的心得博文 - V2EX
Xheldon · 2025-04-08 · via Swift

写得很好,枚举的关联值很好用的,在实际项目中也是在大量使用。
举个栗子,处理 IM 的自定义消息可以这样写:
由于枚举的特性,所有写 switch 的地方(如果没有 default ),会编译报错,这样在新增自定义消息类型的时候不会漏掉所有要改的地方。
其次可以把需要用的字段从 JSON 里解析出来,放进枚举方便使用。

enum IMMessageType {
/// 0 (普通文本)
case text(content: String)
/// 1 (详情)
case detail(id: Int, name: String)

init(from json: JSON) throws {
guard let messageType = json["messageType"].int else {
throw CommonAPIError.invalidResponsor
}
switch messageType {
case 0:
self = .text(content: json["content"].stringValue)
case 1:
self = .detail(id: json["detailID"].intValue, name: json["detailName"].stringValue)
default:
throw CommonAPIError.invalidResponsor
}
}

var displayText: String {
switch self {
case .text(let content):
return content
case .detail(_, let name):
return "详情-\(name)"
}
}
}