easy5 pts · first answer countsswiftjson-parsing
Decoding JSON with extra keys
What does this Swift code print?
struct User: Codable {
let id: Int
let name: String
}
let json = #"{"id": 7, "name": "Sara", "age": 30}"#.data(using: .utf8)!
let user = try JSONDecoder().decode(User.self, from: json)
print(user.name)Discussion 0
Join free to post in the discussion.
Related questions
- Safely unwrapping a Swift optionalMobile App Developer
- Swift struct assignment copiesMobile App Developer
- Swift class shared referenceMobile App Developer
- Memory leak with a closureMobile App Developer
- unowned self in a network callbackMobile App Developer
- When a Swift array is copiedMobile App Developer