UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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 RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more 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.