|
Hi all, I've been working through the 100 Days of SwiftUI course and am trying to stretch my API legs as an extension of the Friendface project (Day 54). I'm struggling when decoding a dictionary, rather than an array as we have in the Friendface project. The URL at the link (https://jsonplaceholder.typicode.com/todos/1) only has four items: { "userId": 1, "id": 1, "title": "delectus aut autem", "completed": false } The code doesn't generate any errors, but when I hit run I see the "Download failed" error printed. I suspect this is because I'm attempting to decode a [User].self array rather than a User.self dictionary, but making that update (and a similar update to the @State var) sets off a series of errors. Any help appreciated, I've spent enough time on this to feel pretty dumb right now.
|
|
That is exactly the problem. What errors are you seeing pop up as a result? This works just fine:
|
|
Brilliant, thanks! Thanks for the quick code rehab. The errors I was seeing on my code before when attempting my dictionary conversion... On the List code: "Initializer 'init(:rowContent:)' requires that 'User' conform to 'RandomAccessCollection'" On the Guard code: "Cannot convert value of type 'Binding<Subject>' to expected condition type 'Bool'" "Referencing subscript 'subscript(dynamicMember:)' requires wrapper 'Binding<User>'" "Value of type 'User' has no dynamic member 'isEmpty' using key path from root type 'User'" I suspect for the List error, List is incompatible with a dictionary as it's meant to serve up an array of data? Guard might be a similar story - it's meant to check if there's an empty array of data - but this is a dictionary which must not have the same "isEmpty" checker? Last question for you - is it required to set initial values for the 'user' var, as you did in your @State above? It looked like for the Friendface project, Paul set his initial array as empty. When I try pulling those initial conditions out of my @State private var user (so, user = User()), Xcode complains it needs a value - fair. Then when I try "user: User" instead, the errors show up in the Previews section ("'ContentView' initializer is inaccessible due to 'private' protection level", "Missing argument for parameter 'user' in call"). |
|
Yup. You don't have a
Yes, the particular
The way it's set up right now, You could change things around so that Which way you choose to go is up to you. |
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.
Sponsor Hacking with Swift and reach the world's largest Swift community!
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.
Link copied to your pasteboard.