Programming in Swift · Challenge: Structures | Ray Wenderlich


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

Hello, thank you for this challenge.
I have a couple of questions:

  1. would it be plain wrong to create a Pizza struct with toppings declared as an [String] to let more freedom?
  2. I have tried this for the overlapping method but I am not sure why this is not consistently good:
    func willOverlap(with restaurant: Restaurant, to location: Location) -> Bool { return self.willDeliver(to: location) == restaurant.willDeliver(to: location) && self.willDeliver(to: location) == true }

Thank you

  1. Not necessarily! But if you know at compile time exactly what all of your options are going to be, for a type, then an enumeration will be easier to work with.

That goes for most everything in Swift, actually. In the second part of Saving Data in iOS, we go over how your Swift types can be represented by formats that are very much akin to working with only Swift Strings, Dictionaries, and Arrays. But making use of Swift’s type system makes for a much better experience.

  1. Your code simplifies to:
self.willDeliver(to: location) && restaurant.willDeliver(to: location)

Whether both restaurants will deliver to a certain location might be useful information to know, but it is different information than whether their delivery areas overlap.

1 Like

great!
Thank you so much!

All the best for the holiday season!!

1 Like

How to understand what exactly do you expect to use in tasks?
Because for example:
“Write a struct that represents a pizza. Include toppings, size and any other option you’d want.”

So I did like that:
struct pizza {
let topping: String
let size: Int
}
Can I use this one as well for task? This is structure and it includes topping and size let as requested.

1 Like

If you don’t want any other options for your pizza, that’ll do just fine! It meets the criteria.

We used an enumeration for toppings because of knowing at compile time exactly what the options would be – enumerations are a perfect match for that.

There seems to be some missing tutorials. The tutorial for structures along with its respective challenge are extremely complex to understand it feels like Ive missed some tutorials in preparation for this - which I didn’t.

Please provide some supporting materials or at least references to other recommended trainings to better understand those concepts. I might be slower than others, but from my perspective, the tutorial is extremely complex and doesn’t help simplify the concept.

Any chance I might be getting a response some time soon?

Hello @fouadghandour,

We’re aware that devoting more time to structures will help out a lot of folks, and we’ll be doing that in this year’s course/learning path update.

In the meantime, I recommend:

  1. Asking any questions you might have, here, whose answers would help clarify anything in the episode for you. (Then we’ll know better where people are struggling, so we can target those area in future updates.)
  2. Reading the official documentation. There are a lot of resources out there, but this is a common one every Swift programmer ought to be familiar with, for all topics.
  3. Searching the web for more info on any topic of confusion you have. This is a forever-ongoing process for all of us. We at raywenderlich.com want to be creating the best learning material, but we can’t cover everything. If there’s some really good information that you find elsewhere which you think we should incorporate into one of our courses, we’d always love to hear about that!

Hi,

this is my second comment I am writing. I am really not a comment-guy, as it were, but I feel I ought to provide some insight as a professional teacher.

  1. The spoken language, in particular with videos, must match what is being illustrated. It is no good to come out with statements like: “You can use self here, but you don’t need to” or “I don’t know, if you have come across nested types or not” without providing thorough explanation. This only creates frustration for the learner, because it is expert discourse. The learner for a lack of knowledge is not part of that discourse group and hence lack out his depth. To give you an analogy, it is as if I were to teach children about American history but in the exam ask questions about English history. So if you were to talk about aspects, which might lead you off on a tangent, I am afraid you would need to provide illustration and, even better, practice material. As it is you are confusing unnecessarily the learner.

  2. You guys need to work out what your customer segment is. Are you providing lessons to newbies or to trained computer experts. Because it does make a difference. Whereas the first videos were easy to follow for me, you really changed the narrative in these videos. It is as if you were talking to somebody completely different (see the statements I am referring to above). As a paying customer I find this frustrating and your comments for other users, experiencing similar difficulties, to browse the internet for answers is not helpful, given a) that Swif is in its 5th incarnation and hence the documents and sites often may refer to older versions without making it explicitly clear, b) is RW not trying to position itself as the best online teacher for Swift?

Didactics and corporate strategy require a conscientious approach. As it is I find it wanting.
Cheers,
Jeremy

1 Like

@jezza Thank you for your feedback - much appreciated! :]

This one, especially, dove pretty deep for a novice. I understand needing to meet the requirements of all skill levels but it would be awesome to provide a few more challenges that are not quite as rigorous. I think that would go much further to ensure a sense of progress and accomplishment for everyone.

Regardless, excellent content as always!

1 Like

I was going through the struct section of the course and I was a bit confused on the `func deliveryAreaOverlaps(restaurant: Restaurant) → Bool {
return location.getDistance(restaurant.location) <=
deliveryDistance + restaurant.deliveryDistance

}` portion. specifically the deliveryDistance + restaurant.deliveryDistance. Why are we adding deliveryDistance + restaurant.deliveryDistance? What does deliveryDistance refer to since right now it’s just a variable within the struct. restaurant.deliveryDistance makes more sense because it’s the actual restaurant’s delivery distance but I’m not understanding how that operation actually happens.

Hi Nicolas!

Why are we adding deliveryDistance + restaurant.deliveryDistance?

That’s the farthest the two restaurants can be from each other if their delivery distances overlap at at least a single point. e.g. Two restaurants 10 miles away from each other, each with a delivery distance of 5 miles, are considered to have overlapping delivery areas. If the restaurants are closer than that to each other, then their delivery areas overlap more.

What does deliveryDistance refer to

It’s how far a restaurant is willing to send someone to deliver a pizza.

restaurant.deliveryDistance makes more sense because it’s the actual restaurant’s delivery distance

restaurant.deliveryDistance is the other restaurant’s delivery distance. deliveryDistance is a shortcut for self.deliveryDistance, i.e. the delivery distance of the restaurant whose method you’re calling.

I am still trying to wrap my head around your codes logic. Is this an ok way to think about the deliveryAreaOverlaps method?

Give one restaurants delivery distance to the other (so one delivery distance enlarges, and the other reduces to 0) and see if the new delivery distance reaches the other restaurant?

Or if you could also explain another way I would appreciate it.
I understand the code completely but the logic of how this works is tricky for me.

Thanks!

Your method works mathematically, but it doesn’t map to a physical concept. (That’s okay! It’s just not as easy to talk about.)

It may be helpful to visualize what a delivery area looks like, using the taxicab geometry we’re working with:

Imagine that the blue dot is a restaurant’s location, and the red dots are the extents of its delivery area. Then, on a larger grid (either imagined or actual), draw another similar diagram of a restaurant struct at a different location. If the rotated red squares overlap, then it is mathematically and physically required that the sum of the delivery distances is greater than or equal to the distance between the restaurants.

If the red squares do not overlap, then it is required that the distance between the two restaurants is greater than the sum of their delivery distances.

This is especially easy to count when the restaurants are axis-aligned with each other either vertically or horizontally.