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

Swift中SelectedDelieveryTimeDateSloteDS不遵循Decodable协议问题排查

「Type 'SelectedDelieveryTimeDateSloteDS' does not conform to protocol 'Decodable'」错误原因及修复

这个错误的核心是你的SelectedDelieveryTimeDateSloteDS结构体中存在不符合Decodable协议要求的成员,具体原因如下:

1. [Any?]类型不支持解码

结构体中customerGroupIDS被定义为[Any?],而Decodable要求所有成员类型必须是可解码的。Any是不确定类型,Swift的JSON解码器无法推断如何将JSON数据解析为Any,必须替换为具体的可解码类型(比如[Int?]或[String?],取决于API返回的实际数据类型)。

2. DayLimitDS未遵循Decodable协议

dayLimits是[DayLimitDS]数组,如果DayLimitDS本身没有声明遵循Decodable协议,父结构体也无法满足Decodable的要求。必须确保DayLimitDS同样实现Decodable(如果JSON键和属性名不匹配,还需要定义对应的CodingKeys)。

3. 潜在的键名不匹配问题

虽然你定义了CodingKeys,但需要确认API返回的JSON键名是否和CodingKeys完全对应。比如storeIDS、customerGroupIDS这些属性,要检查JSON中是否使用了相同的键名(比如是否是store_ids而非storeIDS),不匹配的话需要在CodingKeys中指定原始键。


修复示例代码

// 先确保DayLimitDS遵循Decodable(根据实际JSON结构调整属性)
struct DayLimitDS: Decodable {
    let day: String
    let maxOrders: Int
    
    enum CodingKeys: String, CodingKey {
        case day
        case maxOrders = "max_orders" // 示例:匹配JSON中的键
    }
}

struct SelectedDelieveryTimeDateSloteDS: Decodable {
    let method: String
    let entityID: Int?
    let name, methods: String?
    let active: Bool
    let sortOrder, futureDaysLimit, startDaysLimit, shippingMethodsChoiceLimiter: Int?
    let workingDays, cutOffTime: String?
    let quotesScope: Int?
    let storeIDS: [Int]
    // 替换[Any?]为具体可解码类型,这里假设是[Int?]
    let customerGroupIDS: [Int?]
    let dayLimits: [DayLimitDS]
    
    enum CodingKeys: String, CodingKey {
        case method
        case entityID = "entity_id"
        case name, methods, active
        case sortOrder, futureDaysLimit, startDaysLimit, shippingMethodsChoiceLimiter
        case workingDays, cutOffTime, quotesScope
        case storeIDS, customerGroupIDS, dayLimits
    }
}

内容的提问来源于stack exchange,提问作者Muhammad Rehan

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.11 22:58:10