|
I am not quite understanding why this data variable is declared as this: "let (data, _) = try await..." rather than: "let data = try await..." Is it becaue there are multiple variables being declared or something? Why is this syntax necessary? What does the _ represent? |
|
If you check the Apple Documents for URLSession.shared.data(from: ) you will see that it return |
|
@MeruemMoniker wonders about return types:
Grocery CheckoutI visited the green grocers this week, at the checkout I received my bag of groceries, PLUS a paper receipt. So in pseudocode, it might look like this:
But most of the time, I am not bothered with the receipt, and I typically ignore it by dropping it in the rubbish bin. The underscore character represents the digital rubbish bin when a method offers you a value that you don't care about.
In the case of Cupcake Corner, the In a bona-fide shipping AppStore application, you'll want to capture the Alas. Project 10 is just a teaching application so it focuses just on the returned data, ignoring the server response. Don't Care about the ResponseSo when this method returns a Keep Coding! |
|
https://www.hackingwithswift.com/forums/100-days-of-swiftui/project-10-part-1/26130/26131 Okay, this is all fine. But something else is interesting:
I may be getting sidetracked, but I'm truly wanting to understand better. P.S. Nice analogy. Cemented the idea for me. |
|
@twoStraws noted that a tuple is similar to a struct, but it doesn't have a name.
We don't think of tuples very often in common daily logic. Often if we think we need an answer to a function call, we'll just return a struct that carries all the data we need. A tuple allows the flexibility to return a simpler form of the data, with additional data if necessary. Keep Coding |
|
If you use the form In this case, the tuple has two parts. Because you didn't give the two parts names, the compiler allows this form:
ReviewThis might be a good time to review one of @twoStraw's articles. See-> Structs versus Tuples Integer ExampleHere's an example of a tuple method on
Keep CodingGrowing up, I thought quicksand was going to be a big problem in my life. I knew about quicksand, and possible ways to escape its terrible powers. Yet, to this day, quicksand has had very little impact on me. Tuples might be in the same family. Know about them, consider them. You probably won't interact with them much. If you've read this far, please add to our understanding and give us an example of how you might use |
SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's all new Paywall Editor allow you to remotely configure your paywall view without any code changes or app updates.
Sponsor Hacking with Swift and reach the world's largest Swift community!
You need to create an account or log in to reply.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.