Swift Generics for Beginners - Eliminate Code Duplication

Поделиться
HTML-код
  • Опубликовано: 24 дек 2024

Комментарии •

  • @seanallen
    @seanallen  Год назад +1

    Want to go deeper with Swift and iOS Dev? Check out my iOS Dev courses - seanallen.teachable.com

  • @Ni7ram
    @Ni7ram Год назад +4

    hey sean! i really really appreciate the chill and slower talking.. i know everyone talks super fast on YT , but on programming videos and such giving time to think is muuch needed. great video! very well explained 👏

    • @seanallen
      @seanallen  Год назад +1

      Thanks. I usually get comments about speaking too fast 😂

  • @omnikong
    @omnikong Год назад +4

    Finally, I understand Generics! Thanks, Sean.

  • @JasonMitchellAZ
    @JasonMitchellAZ Год назад +1

    Great explanation and examples

  • @vickywang5049
    @vickywang5049 Год назад +3

    Well explained the usage of generic type! However, when I watch this video, I was expecting to hear about the difference of function with generics (similar to "template" in C++) vs. function with protocol-typed parameters. Basically, Swift allows both to achieve polymorphism. The following code would have exactly same outcome with driveHome() and genericDriveHome():
    protocol Drivable {
    func drive()
    }
    struct Motorcycle: Drivable {
    func drive() {
    print("I'm a motorcycle. I'm going home.")
    }
    }
    struct Car: Drivable {
    func drive() {
    print("I'm a car. I'm going home.")
    }
    }
    func driveHome(vehicle: Drivable) {
    vehicle.drive()
    }
    func genericDriveHome(vehicle: T) {
    vehicle.drive()
    }
    let m = Motorcycle()
    let c = Car()
    driveHome(m)
    genericDriveHome(m)
    driveHome( c)
    genericDriveHome( c)
    In fact, if I use the same func name:
    func driveHome(vehicle: T){
    vehicle.drive()
    }
    and call driveHome(m), the protocol typed function is favored.
    .
    .
    .
    I found the answer on StackOverflow, if you have the same question as me: stackoverflow.com/questions/38446487/what-is-the-in-practice-difference-between-generic-and-protocol-typed-function-p/41490551#41490551

  • @danstoian7721
    @danstoian7721 Год назад +3

    1:50 So when to use generics as opposed to just "some Drivable"?

  • @saeedrahmatolahi462
    @saeedrahmatolahi462 Год назад +1

    Sean allen you are the best, explain everything very nice and simple so everyone can understand it easily 😊👍

    • @seanallen
      @seanallen  Год назад

      Thanks for the kind words, Saeed.

  • @ruslangaliev5050
    @ruslangaliev5050 Год назад +1

    Thank you for the video!
    You explain complex things in simple terms!

    • @seanallen
      @seanallen  Год назад

      Glad you enjoy them, Ruslan!

  • @carlosswiftdev2703
    @carlosswiftdev2703 Год назад +1

    Good video :) you can use the max(_:_:) function which takes two Comparable types.
    print(max(valueOne, valueTwo), "is the highest value")

    • @seanallen
      @seanallen  Год назад

      I knew there was a better way.

  • @bosch5053
    @bosch5053 Год назад +1

    Thank you for the video!

  • @rparham1997
    @rparham1997 Год назад +2

    Can you do a video on protocols?

    • @seanallen
      @seanallen  Год назад +3

      It's on my very long video idea list. So many video ideas, so little time :)

  • @yt-1161
    @yt-1161 9 месяцев назад +1

    Which theme are you using in xcode

    • @seanallen
      @seanallen  9 месяцев назад +1

      It's called Midnight

  • @faruuqqayyum3811
    @faruuqqayyum3811 Год назад +1

    Can you do a video for "Opaque Types"?

    • @seanallen
      @seanallen  Год назад

      I'll add it to the idea list. However that list is VERY long :)

  • @roh9934
    @roh9934 Год назад

    How do you choose the protocol? like Decodable, comparable?

  • @rkarnes6304
    @rkarnes6304 Год назад

    Sean, what’s your recommendation for someone to learn Swift coming from a C++ background 25 years ago, then Java 17 years ago. I’ve done virtually no coding since 2006, but want to get back into it for Vision Pro. (Shoot me any questions you have if needing to lock down further background information before making a recommendation.) Thanks much! I really want to do some awesome Vision Pro apps.

  • @mipmap256
    @mipmap256 5 месяцев назад

    One thing I don't understand, since you provide protocol for Generic type, why not just use protocol directly.

  • @mdtarifkhan1825
    @mdtarifkhan1825 Год назад +1

    ❤❤