Posts

Post not yet marked as solved
1 Replies
179 Views
struct Meta: Codable { let isEnd: Bool enum CodingKeys: String, CodingKey { case isEnd = "is_end" } } let jsonStr = """ { "is_end" : true } """ let jsonData = jsonStr.data(using: .utf8)! do { let decoder = JSONDecoder() decoder.keyDecodingStrategy = .convertFromSnakeCase let result = try decoder.decode(Meta.self, from: jsonData) print(result) } catch { print(error) } Code written in the above manner will result in the corresponding error. keyNotFound(CodingKeys(stringValue: "is_end", intValue: nil), Swift.DecodingError.Context(codingPath: [], debugDescription: "No value associated with key CodingKeys(stringValue: \"is_end\", intValue: nil) (\"is_end\").", underlyingError: nil)) I think there is nothing wrong with the code. But why is the error occurring? And, Can't the enum be named something other than CodingKeys? like this, struct Meta: Codable { let isEnd: Bool enum CodingKeyss: String, CodingKey { case isEnd = "is_end" } } And these codes return normal results. What's the reason Meta(isEnd: true)
Posted
by Tag_.
Last updated
.