ios - 我可以在 Codable 中使用枚举吗?
问题描述
我创建了一个枚举
enum FeedItemType: String {
case selling = "SELLING"
case news = "USER_NEWS"
}
该字符串与type
我的 JSON 响应中返回的属性匹配。
在使用我正在创建的结构时,我希望能够在我的代码中的其他地方使用枚举值。这可能吗?
我正在尝试这样的事情
struct FeedItem: Codable, Equatable {
var type: FeedItemType
private enum CodingKeys: String, CodingKey {
case type
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.type = try container.decode(FeedItemType.self, forKey: .type)
}
}
但是我遇到了很多错误
没有“解码”候选产生预期的上下文结果类型“FeedItemType”
类型“FeedItem”不符合协议“Encodable”
解决方案
推荐阅读
- android - 我怎样才能让用户购买消耗品并给予他们非消耗品作为奖励?
- javascript - 如何通过javascript使django中的边栏中的元素处于活动状态包含标签?
- nginx - 如何编写 Nginx 反向代理?
- node.js - 如何从 nodeJS 中的 URL 获取文件,构建 zip 文件并通过管道传输到云存储桶
- next.js - Nextjs API - 未传递查询参数
- javascript - 如何在nodejs中从本地文件夹获取调整大小的图像到服务器
- php - 订单成功后更新数据库中的产品数量
- java - 为什么我的布局文件没有链接到我的微调器?
- c++ - 有什么方法可以使用 STL 算法在 C++ 中同时打乱两个向量?
- android-studio - Flutter 构建 APK 导致“找不到资源 android:attr/fontVariationSettings”