Group Group Group Group Group Group Group Group Group

Programming in Swift · Structures | Ray Wenderlich

#1

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

#2

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

0 Likes

#3

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

0 Likes

#4

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.

0 Likes

#5

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

0 Likes

#6

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
0 Likes

#7

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

0 Likes

#8

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

0 Likes

#9

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

0 Likes

#10

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

0 Likes

#11

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.

0 Likes

#12

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

0 Likes

#13

Hello!

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

0 Likes

#14

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.

0 Likes

#15

Hi there,

I really appreciate the effort you guys put in to make things clear… but seriously, what’s up with the Restaurant example of struct???

I thought you strive for making things easier …not to confuse people with structures. Maybe I’m dumb, but there are easier ways for explaining concepts.

After watching your video, I feel like Apple documentation (where some topics are hard to navigate through) is easy peasy.

The complexity of the examples you use in your presentations, will be associated with the concept you’re trying to explain. If you confuse your viewers with complicated examples of structs, we perceive the entire concept as being complex and difficult to remember.

Example: extensions. Why on earth did you had to present the new concept (aka extensions) alongside with a computed property and a closure ?? Couldn’t you guys find an easier example for an extension? such as, showing how an extension can be used when … well, extending a struct or a class (file), for adding a property which is not computed?

0 Likes

#16

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

0 Likes

#17

Hi Cosmin,

I decided to go with Paul Hudson and his “Hacking with Swift” bundle of books and Youtube videos. I find his materials easier to understand.
Don’t get me wrong: you guys do a wonderful job at what you do and I like the variety and richness of content.

But your Swift course (some parts of it, at least) appears to be taught by developers and not by teachers / instructors.

Regards,
Petre

0 Likes

#18

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

0 Likes

#19

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!

0 Likes

#20

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?

0 Likes