草庐IT

clubLevel

全部标签

swift - 可解码,不解码具有无效值的可选枚举

我定义了这样一个枚举:enumClubLevel:Int,Codable{casegolden=1,silver,bronze}在我的结构中,我有一个类型为ClubLevel的可选属性,当我在init(fromdecoder:Decoder)中解码此属性时:self.clubLevel=trycontainer.decode(ClubLevel?.self,forKey:.clubLevel)我遇到这个错误:调试描述:“无法从无效的Int值0初始化ClubLevel”,underlyingError:nil“我想知道即使这个属性是可选的,解码器也不会继续有什么想法吗?