|
Hello, trying to figure out why my initial fetch won't happen. It was working until I decided to hold an array of my User structs as a property in a class. In the line
|
|
I'm unclear why you want a User struct inside a Users struct (not a class as written). To me, that's deeply confusing. Usually, users would refer to an array (or set) of the User objects. You'd need to have Users as a whole also conform to codable in the case you've given, though. Here's a link about Codable conformance. |
|
Hey, thanks so much for your response!
I modeled this after challenge #3 from the cupcake app, which worked well for that, so I thought it might work well here too. That involved nesting a struct called |
SPONSORED Still waiting on your CI build? Speed it up ~3x with Blaze - change one line, pay less, keep your existing GitHub workflows. First 25 HWS readers to use code HACKING at checkout get 50% off the first year. Try it now for free! Sponsor Hacking with Swift and reach the world's largest Swift community! |
|
Hi @jessilinden,
I'm not sure about the asyn error, but I believe what you're running into with the Users class Codable conformance is talked about here: Just because the structs in the class are codable, doesn't make the class codable. And he goes through the exercise of making it confrom to codable by using CodingKeys. But it seems easier - and cleaner - to just separate out the User struct in another file. Also, you have" @Published var items: [User] I'm wondering why it's [User] and not [User]() ? Does that make a difference? |
|
Thank you both for your responses! Even though I did add Codable conformance to that class and the 2 errors from loadData() went away, I decided to start fresh, keeping it simple to start. I made a file with my User and Friend structs: <br>
and ContentView, which has the loadData() method. The code compiles, but the fetch fails. Spot anything wrong with it? Based on some print statements I experimented with, I can tell that it's the JSONDecoder().decode line that isn't working. The print statement went right below that, and I only got the Fetch Failed: Unknown Error statement. Perhaps worth mentioning that when I did the tutorial with the Taylor Swift albums that were supposed to get decoded from iTunes, same problem: compiles, but no decoding. When I did the Cupcake Corner app, it involved encoding first, so the decoding worked.
|
|
Hi again, I copied your code and ran it. (I'm just starting this project too!). I got the same ' Fetch Failed: Unknown Error'. I then chaned the registered type from Date to String - and it worked. I haven't looked into why the Date didn't work, or even what the format yet is in the JSON. (haven't gotten very far in the project :) ). I also tried using Date? for registered, and got teh same Fetch Failed Error. My plan is to read the Date as a string, do whatever conversion/formatting I need after I decode the JSON. |
|
In reviewing my notes, I see the lesson mentions you need to apply a date formatter if you want that field. I guess another option is to just ignore that field, if you don't want to show the date. (But if you do keep in the Date and format it, it should be a Date Optional, I think, too. Just in case). |
|
If you want to see my reply on Day 60: error 403 when trying to fetch friendface.json thread it the same question. The date is
|
|
Thanks, Nigel! And then ... the formatter I mentioned above would be used if we used the dates in our app and wanted them formatted, I assume? |
|
|
|
Hey, thanks to you both! Got the app functioning now and that date solution is so succinct. Just out of curiosity, do you have a suggestion for how you might handle an optional Date as the type for the property registered? I just experimented with something like this, inspired by some of Paul's other code, but obviously I gave a String as the default value, which won't work here since it's a Date.
I'd also welcome anyone's insight on first(where:) to connect the name of the Friend to the rest of the details for THEIR User info. Equatable as another option? I see where we did first(where:) in Moonshot, but it's in an initializer... Assuming you could put it in a function as well, but a little lost on the smoothest approach here. |
|
Facing same issue! If I add an expense and then tap Edit the red circles do not show up, but Edit does change to Done in the NavBar. Then if I swipe on any item to expose the row's delete option on the far right side, and then either commit the delete or cancel out of it, then tap edit the delete circles show up. |
SPONSORED Still waiting on your CI build? Speed it up ~3x with Blaze - change one line, pay less, keep your existing GitHub workflows. First 25 HWS readers to use code HACKING at checkout get 50% off the first year. Try it now for free!
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.