Advanced Swift: Generics and Protocols · Associated Types | raywenderlich.com


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/1940187-advanced-swift-generics-and-protocols/lessons/4

Hey, is there an issue with the “Associated Types” video? It doesn’t seem to want to load for me in Brave or Chrome, as the play button never appears. All other videos appear to be playable as expected.

Yes, video doesn’t load for me either… Pls fix :wink:

Poke, poke to @rayfix

Same here. No joy on video 4.

It is loading for me in Safari and Chrome but it actually looks like it is the “non-optimized” version without edits to make it run a little faster. I will find out what is going on with that tonight.

Still “Associaited Types” video is not loading. when is expected to fix

Great tutorial, Sir if possible use some simple examples as in AssociatedTypes video, the bell curve example is a bit hard to understand the concept that we are going to learn.

I feel it is great course, but I need to repeat multiple times to get​:man_technologist::sweat_smile:
Thanks again!

Hi @jpsasi This video was fixed on the 9th, but it looks like you commented on the 10th that it still wasn’t playing for you. I’m not able to find any playback issues now. Can you please try to watch the video again and if it’s not working for you, please send us an email at support@razeware.com and let us know whether the problem is specific to one browser and if so which one are you using. Or if you can’t get it to play on any browser, please let me know that too. Hopefully it’s working fine for you, but please email us if not.

It is always hard to come up with examples. Sorry if this one didn’t speak to you. :wink:

The idea is that you have a protocol. Sometimes you want to return a random Int. But sometimes you want to return a random Double. Semantically, the two are the same. Rather than make a whole new protocol you can express it with an associated type. Value is used as a type that stands in for Int or Double.

With that understanding, (and not worrying much about how the values are computed), see if it makes sense. As a side benefit, now you know something about the cool Box-Muller algorithm.

I had the same problem with focusing on the subject matter but your usage of obscure syntaxes, like precondition that I’ve never seen anyone use, is very helpful

1 Like

The example chosen (distributions) greatly distracts from the subject at hand. All you wanted to say is that you can implement this protocol using Int and Double.

Sorry that the example didn’t speak to you. Distributions form nice hierarchies but they are a bit abstract. I could have used MagicUsers that return Fireball and LightningBolt types but wanted to use something I thought you could encounter in real life. I will think about your advice for next time. What example do you think you would have used? (Thanks again for the feedback.)

No biggie, I definitely appreciate your effort. I would have used a very simple example, a very very boring one, one that needs no real mental effort to consume. Instead of paying attention to how you implemented this protocol, I was trying to wrap my mind around distributions.

Now I am ready for maths olympiad :smile:
The example was more helpful than the subject.

@coolwinksteam Thank you for your feedback - much appreciated!

Hello everyone,

This was a really good concept. I tried experimenting with the concept myself and I think it was fairly simple based on the sample project once you get your hands on the code. @rayfix @shogunkaramazov Yet another advanced concept that got me excited. Here’s what I tried and felt proud of being able to take away from this video:

import Foundation

protocol RequestManager {
    associatedtype Address
    
    func getWebAddress() -> Address
}

struct HomePageNetworkManager: RequestManager {
    typealias Address = String
    
    func getWebAddress() -> Address {
        "https://www.my3vshenoy.com"
    }
}

struct BlogNetworkManager: RequestManager {
    typealias Address = URL
    
    func getWebAddress() -> Address {
        URL(string: "https://www.my3vshenoy.com/blog-1")!
    }
}

let homepageNetworkManager = HomePageNetworkManager()
let homepageUrlString = homepageNetworkManager.getWebAddress()

let blogNetworkManager = BlogNetworkManager()
let blogPageUrl = blogNetworkManager.getWebAddress()