|
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.
Run this in Playgrounds and 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 In other words, make your 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. More BehavioursIf you want to show your fruits in a This doesn't require a function. Keep Coding! |
|
@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 Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills β itβs the fast track to being a complete senior developer! Hurry up because it'll be available only until October 1st.
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.