GO FURTHER, FASTER: Try the Swift Career Accelerator today! >>

SOLVED: day 39 question

Forums > 100 Days of SwiftUI

small question about day 39. heres the code that i have an issue with:

let data = Data(input.utf8)
let decoder = JSONDecoder()
if let user = try? decoder.decode(User.self, from: data) {
    print(user.address.street)
}

1) what does Data(input.utf8) mean? does it transform json into utf8? 2) i forgot what decoder parameters were exactly, is User.self specifying where we want to put the decoded data into? and is data what we are decoding?

Thank you very much, this forum is amazing.

2      

@Bnerd  

1) .utf8 the encoding of the input Data (i.e. how to read it) 2) User is the struct that decoder will use in order to decode the data

Your struct (User in this case) should match the json you are decoding in order to succesfully return the results.

2      

Hacking with Swift is sponsored by try! Swift Tokyo.

SPONSORED Ready to dive into the world of Swift? try! Swift Tokyo is the premier iOS developer conference will be happened in April 9th-11th, where you can learn from industry experts, connect with fellow developers, and explore the latest in Swift and iOS development. Don’t miss out on this opportunity to level up your skills and be part of the Swift community!

Get your ticket here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.