Programming in Swift · Structures | Ray Wenderlich


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/5994-programming-in-swift/lessons/39

Very difficult to understand…lost myself after 5 minutes of lesson.

2 Likes

Hi! I’m sorry to hear you’re having trouble with this one. Can you tell us what you had trouble understanding?

For example that moment:
restaraunts[0].willDeliver(to: Location(x:1, y: 2))

There are some many “location” words in this topic , so I can’t clearly understand the right connection: step by step.

@ollybess Please let us know what you don’t understand exactly in the above line of code. Thank you!

Ok

  1. restaurants[0] means we’re taking 1st element:
    Restaurant(location: Location(x: 0,y: 0), deliverDistance: 3)
  2. For this element we’re executing func willDeliver…
    and after that I’m lost, what values and objects exectly we take and what happen next

@ollybess Do you have issues with what the willDeliver(to:) method does exactly?

that’s correct
too many “location” inside that method

@ollybess The willDeliver(to:) method is related to the getDistance( _: ) one. Do you understand what that one does?

I understand what we get using that method, but did’nt clear understand how.
and how they work together (willDeliver and getDistance)

Very hard to follow this tutorial. Swift tutorials were okay to follow until it got to closures and structs. Should start with simpler examples and then build up. Keep in mind people who are watching this tutorial aren’t programmers already. Also, for the harder topics such as the ones mentioned, break the videos down into shorter ones.

@mostafa.gazar Thank you for your feedback - much appreciated! Could you please let us know what you found hard to follow exactly?

Hello!

I agree with mostafa.gazar the example is difficult to follow. Additionally I did not get what is “extension”

Hi! As mentioned in the video, extensions allow you to add code to a struct outside of its original definition. So all of the code we wrote in the extension, you could move back into the struct’s definition. You’ll see extensions used a lot as an organizational tool in other courses and in tutorials on the site.

@peter0123 Please let us know what you find difficult to understand when you get a chance. Thank you!

Hi,

I got through until you guys starting explaining about ‘extensions.’ I understand that the core function of using ‘extension’ is to extend the ‘struct Location’ somewhere else and put some more code inside that same struct. But I was lost when you guys started explaining about ’ var canGenPizzaDelivery: Bool { return… ’

Please correct me if I am wrong.

  1. You guys used ‘var’ in front of ‘canGetPizzaDelivery’ because it can be both ‘true’ or ‘false’

  2. ‘canGetPizzaDelivery’ is of value type ‘Boolean’ because it can have either ‘true’ or ‘false’

  3. you guys will return ‘true’ if restaurants array contains any value and ‘false’ if restaurants array contains no value

  4. And then a closure where its input name is restaurant (and im confused from this point)

  5. And the closure will test out your restaurants will deliver to a certain location ??

  6. why did you use self?? I don’t understand the usage of self in this particular case. Please explain further.

  7. Where can i find more practices regarding this part?? Closure? Struct??

Thanks

var is the only option for computed properties, even if they’ll always return the same value. If the value would always be the same, you could store it in a constant, but in this case, you can’t consider canGetPizzaDelivery constant. It depends on deliveryDistance, which is variable.

Right, those are the only options.

No. We return whether any restaurant will deliver to the location. One option for reading the code in English is “return whether the restaurants array contains any restaurant that will deliver to this location.”

self is the Location instance which you’re seeing if any restaurant will deliver to.

If you’ve already gone through the first 5 parts of Programming in Swift, for writing closures, I’d recommend checking out Apple’s documentation on the Sequence protocol and the Collection protocol. They have a ton of handy methods which accept a closure as a parameter, all for different purposes. Over time, you’ll gain experience learning how to use them instead of loops, to express yourself clearly.

In our Saving Data in iOS and Data Structures and Algorithms in Swift courses, we make a lot of use of structures. Data Structures is a little more advanced, but checking it out will at least give you an idea of what you have to learn. Hopefully this course will have prepared you enough for Saving Data, at least, but if not, feel free to comment on the videos there and we’ll try to help you out.

Good luck and have fun learning!

Could you expand on the usage of ‘self’ in this case??? What exactly is a location variable? I remember using self when you used ‘x.self’ but i am not quite familiar with using self by itself. Can you give me some more examples?

self is the way to refer to an instance of a type within its own methods, computed properties, and initializers.

I can’t find where this phrase was used, so I’m not sure what might be confusing. What are you referring to specifically?

Check out the next video after the challenge, on properties. Either that will help you via more practice, or you’ll have more examples to ask questions about!

Hi!

I have to agree! Up to now you made things very easy to understand. But this video just confuses me as I keep asking myself how I would be able to write such code. I don’t find it self-explanatory. When the teacher says things, you can use self here but you don’t need to, I would need to know more. I would want to know why and how it works. Maybe these are things that are difficult to explain in a video. It would be an option to have some more material available as a download.

At the level I am at at the moment, I find the syntax difficult to follow in this video. You keep adding layer after layer and I pausing the video after each sentence trying to make sense of it all. Anyway, I will try and plough through and see if I get the hang of it.