Type Erasure In Swift | iOS Development

แชร์
ฝัง
  • เผยแพร่เมื่อ 28 ก.ย. 2024
  • In this tutorial video, we will explore how to use type erasure in Swift with protocols and composed types. Type erasure is a powerful technique that allows you to hide the underlying type of an object and work with it using a more generic interface.
    We will start by discussing the concept of protocols and composed types in Swift, and how they can be used to create generic types that can work with a variety of different objects. We will then dive into the details of type erasure, and show you how it can be used to simplify your code and make it more flexible.
    Throughout the video, we will be using practical examples to demonstrate the concepts we are discussing. By the end of the tutorial, you will have a solid understanding of how to use type erasure in Swift to create more robust and flexible code.
    Whether you are a beginner or an experienced Swift developer, this video will provide you with valuable insights into the power of type erasure and how it can be used to make your code more flexible and easier to work with. So sit back, relax, and get ready to dive into the world of type erasure in Swift!
    💻 Source Code: / iosacademy
    🎥 Subscribe for more: www.youtube.co...
    😎 Like my teaching style? Check out some of my most popular courses! courses.iosaca...
    👉🏼 Connect (personal LinkedIn) / afrazsiddiqui
    🚀 Follow on LinkedIn / ios-academy
    ** Popular Series
    Building Instagram: courses.iosaca...
    Building TikTok: / @iosacademy
    SwiftUI for Beginners: ios-academy.te...
    ** Get Skillshare free for 2 Months and learn iOS
    www.skillshare...
    ** Manage all your investments from app earnings on Betterment!
    bit.ly/3eBwlI9
    ** Grow your own TH-cam tech channel with TubeBuddy:
    www.tubebuddy....
    #swift #iOSDeveloper #Xcode

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

  • @nX-
    @nX- ปีที่แล้ว +30

    This does not look like type erasing. What you explained is basically how protocols/interfaces work. Type erasing, just like the name is saying, is hiding/removing the original type of a type. An example of type erasing in Apple's own code, is `AnyView` or `AnyPublisher`. Both are concrete value types, but have the same interface of the `Publisher` or `View` protocols. Their goal is to wrap the original type so that you can use these abstractions in Arrays or basically any where where the compiler can't guarantee or resolve the type based on the protocol. So the solution is to use a concrete type, and erase the type of the original one, like so:
    struct AnyView {
    var originalView: Any
    init(view: View) {
    self.originalView = view
    }
    func width() {
    (originalView as! View).width()
    }
    }
    What we are doing here is that we guarantee that in the constructor we pass the correct type, but inside we erase it. This was especially needed if the "View" type in this case had associated types in the Protocol. Nowadays, with the new `any` keyword, this manual type erasing is not needed any more (for the most part).

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

      I agree with you.

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

      The best scenario we use a type eraser is protocol with the associate type being used as properties or parameters.

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

      This was meant to be a simple example

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

      @@rebellious_703 ill do a follow up video

    • @nX-
      @nX- ปีที่แล้ว

      @@iOSAcademy This is a not simple example. Because it is not type erasing. The foundation behind type erasing, is removing the original type with "Any" by making the code "unsafe" inside a Box, so that you can trick the compiler

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

    the author should first study what type erasure in swift actually is

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

    How about using ```public protocol MyPresentableColorChangingController: UIViewController```
    instead of ```UIViewController & MyPresentableColorChangingController``` ?

  • @dmitriymazurenko1229
    @dmitriymazurenko1229 ปีที่แล้ว +2

    This is definately not type erasure. Type erasure examples are AnyPublisher and AnyHashable, this is just protocol compositon...

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

      Protocol composition is indeed a simple form of erasure. Its how Any* is built

  • @sarvarqosimov8363
    @sarvarqosimov8363 ปีที่แล้ว +2

    👍

  • @Stricken174
    @Stricken174 7 หลายเดือนก่อน +1

    Passing UIViewController and any protocol doesn't make sense because you are simply extending the functionality of UIViewController with the protocol. This is not type erasure. Your method requires a specific type to work

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

    Great content, but Im still wondering about that extension in the first viewcontroller. Is there any other way to make it work? Maybe making SecondViewController conform to the protocol?

    • @FloWritesCode
      @FloWritesCode ปีที่แล้ว +2

      That is possible, but the goal is to not import PresenterKit in CustomUI as far as I understood.

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

      Exactly

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

    not sure just usage typealias (to be honest only for syntax sugar) that's exactly type erasure in swift😄

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

    ya learn something new every day. Did not know about this until now ✊ Thanks!

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

    Is this video about VIPER Architecture?

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

    Great video and very helpful

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

    great

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

    Great video

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

    nice