Swift Generics for Beginners - Eliminate Code Duplication

แชร์
ฝัง
  • เผยแพร่เมื่อ 24 พ.ย. 2024

ความคิดเห็น • 28

  • @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.

    • @seanallen
      @seanallen  ปีที่แล้ว

      Happy to hear it!

  • @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

  • @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.

  • @danstoian7721
    @danstoian7721 ปีที่แล้ว +3

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

  • @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.

  • @ruslangaliev5050
    @ruslangaliev5050 ปีที่แล้ว +1

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

    • @seanallen
      @seanallen  ปีที่แล้ว

      Glad you enjoy them, Ruslan!

  • @JasonMitchellAZ
    @JasonMitchellAZ ปีที่แล้ว +1

    Great explanation and examples

    • @seanallen
      @seanallen  ปีที่แล้ว +1

      Thanks Jason!

  • @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 :)

  • @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 :)

  • @yt-1161
    @yt-1161 8 หลายเดือนก่อน +1

    Which theme are you using in xcode

    • @seanallen
      @seanallen  8 หลายเดือนก่อน +1

      It's called Midnight

  • @roh9934
    @roh9934 ปีที่แล้ว

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

  • @mipmap256
    @mipmap256 4 หลายเดือนก่อน

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

  • @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.

  • @mdtarifkhan1825
    @mdtarifkhan1825 ปีที่แล้ว +1

    ❤❤

    • @seanallen
      @seanallen  ปีที่แล้ว

      Glad you liked it!