Skip to content

Instantly share code, notes, and snippets.

@cocoahero
Last active September 21, 2017 14:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cocoahero/232f710f14a7546cffe4dc9773b88c9d to your computer and use it in GitHub Desktop.
Save cocoahero/232f710f14a7546cffe4dc9773b88c9d to your computer and use it in GitHub Desktop.
fatal error: 'try!' expression unexpectedly raised an error: Swift.DecodingError.dataCorrupted(Swift.DecodingError.Context(codingPath: [Foundation.(_JSONKey in _12768CA107A31EF2DCE034FD75B541C9)(stringValue: "Index 3", intValue: Optional(3))], debugDescription: "Cannot initialize Season from invalid String value fall", underlyingError: nil)): file /Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-900.0.65/src/swift/stdlib/public/core/ErrorType.swift, line 181
import Foundation
enum Season: String, Codable {
case winter
case spring
case summer
case autumn
}
let json = """
["winter", "spring", "summer", "fall"]
""".data(using: .utf8)!
// Is there a way to "skip" failing items instead of erroring on the entire array?
let seasons = try! JSONDecoder().decode([Season].self, from: json)
print(seasons)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment