|
Hello, Can someone try to explain the attached code? My code for the Day 47 challenge works, but I don't understand the static func portion of my struct for Equatable:
and I don't understand how the comparison works inside the DetailsView:
Trying to work through this challenge, I had to resort to copyinmg someone's code but don't like that I don't understand it. Please try to explain how this is working. |
|
When you conform a data type to To conform to So here:
you are saing that 2 Here in the
(specifically the you are looping through the list of |
|
Thank you @roosterboy, So, theoretically, is the code not great? For example, the .title could be the same for several
I really appreciate the help. This one has me really confused and I want to ensure I understand before I move on. |
|
You should keep
And you are correct!
That depends. What does it mean for two Once you figure out what makes two You can actually have the compiler synthesize |
|
@IceMonster has questions about Equatable. He received many correct answers from our regulars. I hope I can add some clarification without further confusing Ice? Take a step away from your solution, and think about how you compare two items. Start with Integers. How can you tell if one integer is the same as another? That's trivial! How can you determine if one string is the same as another? Also pretty trivial! But how do you determine if two songs are the same?
Examine the two songs! Are they, in fact, the same song? What would be appropriate rules to declare two songs are the same? If you just compare song titles, you'll have problems with the song named "Try" (Nelly Furtado, John Mayer, Pink). If you compare song title and group, you'll have issues when groups release dance versions, extended versions, instrumental versions, and possible greatest hits versions. Instead, to compare two of your Song It may seem strange to you, but the "name" of that function is And the reason it's a static function? The method belongs to the Keep coding! |
SPONSORED Play is the first native iOS design tool created for designers and engineers. You can install Play for iOS and iPad today and sign up to check out the Beta of our macOS app with SwiftUI code export. We're also hiring engineers!
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.