|
At first swift seemed pretty easy. But, the last couple of days have me begging for mercy. I just barely... barely understand what I'm doing here. But, after a lot of messing around with it and checking past lessons I think I got it working as requested. Do you guys ever feel like you don't know what you don't know? I don't even know what to ask at this point. Ha I learned the shorcuts for highlighting and commenting code in xcode today if achieving nothing else.
|
|
This looks fine to me. It's kind of hard to understand the purpose of protocols when you first learn about them, and just create one for no reason other than that you were told to. But, later on, you will learn how to make your own structs/classes conform to protocols that exist in Swift natively, and then I think the purpose of them becomes a bit more clear. As long as you got the idea well enough to do what you have done for now, I think you'll be in good shape for the future. |
|
@Ostrich has a great observation:
So true! Here's an example from my Playgrounds to help me process Protocols. This is a simple fruit structure:
Simple enough. Now create a simple fruit bowl.
If you run this in Playgrounds, you'll see you have an array of Fruit objects. So far, so good. Now try some common array functions on your fruit bowl.
How do you sort fruit?It's easy to sort This is the strength of Let's conform!So add this to your Fruit playground:
Now, when you run the line below, Swift knows how to sort your Fruits. All you had to do was add a
Sorted ResultsWhen I run the line in Playgrounds I see: This π banana is Yellow. |
|
@Obelix Thank you! This is like a who 'nother lesson on it. I bookmarked this to mess with it in playground later. The fruit salad idea already makes more sense than the buildings. |
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.