Type Erasure In Swift | iOS Development

  Переглядів 7,173

iOS Academy

iOS Academy

Рік тому

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: ukposts.info?su...
😎 Like my teaching style? Check out some of my most popular courses! courses.iosacademy.io
👉🏼 Connect (personal LinkedIn) / afrazsiddiqui
🚀 Follow on LinkedIn / ios-academy
** Popular Series
Building Instagram: courses.iosacademy.io/p/build...
Building TikTok: / @iosacademy
SwiftUI for Beginners: ios-academy.teachable.com/p/s...
** Get Skillshare free for 2 Months and learn iOS
www.skillshare.com/r/user/afraz
** Manage all your investments from app earnings on Betterment!
bit.ly/3eBwlI9
** Grow your own UKposts tech channel with TubeBuddy:
www.tubebuddy.com/iosacademy
#swift #iOSDeveloper #Xcode

КОМЕНТАРІ: 36
@nX-
@nX- Рік тому
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 Рік тому
I agree with you.
@rebellious_703
@rebellious_703 Рік тому
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 Рік тому
@@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
@Landon_Hughes
@Landon_Hughes Рік тому
ya learn something new every day. Did not know about this until now ✊ Thanks!
@iOSAcademy
@iOSAcademy Рік тому
🚀🚀
@aecollector168
@aecollector168 Рік тому
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 Рік тому
That is possible, but the goal is to not import PresenterKit in CustomUI as far as I understood.
@iOSAcademy
@iOSAcademy Рік тому
Exactly
@angeloandradecirino3386
@angeloandradecirino3386 Рік тому
Great video and very helpful
@iOSAcademy
@iOSAcademy Рік тому
Thanks
@samha1513
@samha1513 Рік тому
Great video
@iOSAcademy
@iOSAcademy Рік тому
Thanks
@hackenbacker
@hackenbacker Рік тому
How about using ```public protocol MyPresentableColorChangingController: UIViewController``` instead of ```UIViewController & MyPresentableColorChangingController``` ?
@iOSAcademy
@iOSAcademy Рік тому
Thatll work too
@shinitiomit1095
@shinitiomit1095 Рік тому
Is this video about VIPER Architecture?
@iOSAcademy
@iOSAcademy Рік тому
Nope
@chhitsakareach7708
@chhitsakareach7708 11 місяців тому
great
@iOSAcademy
@iOSAcademy 11 місяців тому
Thx
@victorriurean
@victorriurean Рік тому
nice
@iOSAcademy
@iOSAcademy Рік тому
Thanks
@sarvarqosimov8363
@sarvarqosimov8363 Рік тому
👍
@iOSAcademy
@iOSAcademy Рік тому
Thanks
@artemsemavin9142
@artemsemavin9142 Рік тому
not sure just usage typealias (to be honest only for syntax sugar) that's exactly type erasure in swift😄
@iOSAcademy
@iOSAcademy Рік тому
Yep
@Stricken174
@Stricken174 3 місяці тому
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
@gribovmax
@gribovmax 9 місяців тому
the author should first study what type erasure in swift actually is
@dmitriymazurenko1229
@dmitriymazurenko1229 Рік тому
This is definately not type erasure. Type erasure examples are AnyPublisher and AnyHashable, this is just protocol compositon...
@iOSAcademy
@iOSAcademy Рік тому
Protocol composition is indeed a simple form of erasure. Its how Any* is built
@amin-o-acid
@amin-o-acid Рік тому
Umm... What? How is this type erasure? Kuch bhi
@iOSAcademy
@iOSAcademy Рік тому
Things like AnyCollection & Any* are designed with such constructs
@amin-o-acid
@amin-o-acid Рік тому
@@iOSAcademy Fair enough but in the video you didn't allude to this nor elaborate on it. Don't mind me saying, most of your stuff is good but here calling an egg a boiled egg before boiling is a bit... premature.
How to use phantom types in Swift
25:00
Paul Hudson
Переглядів 13 тис.
Prefetching with TableViews (2022) - iOS
16:35
iOS Academy
Переглядів 18 тис.
Артем Пивоваров х Klavdia Petrivna - Барабан
03:16
Artem Pivovarov
Переглядів 6 млн
0% Respect Moments 😥
00:27
LE FOOT EN VIDÉO
Переглядів 37 млн
How to use Generics in Swift | Advanced Learning #8
19:24
Swiftful Thinking
Переглядів 13 тис.
Swift Optionals - How to Unwrap (real examples)
14:20
Sean Allen
Переглядів 10 тис.
Opaque Types in Swift Explained (2023) - iOS
7:48
iOS Academy
Переглядів 9 тис.
Diffable Data Source (Swift 5, Xcode 12, TableView) - iOS 2020
16:54
iOS Academy
Переглядів 16 тис.
Protocols With Associated Types vs Generics (Swift 5)
12:21
SwiftBook
Переглядів 6 тис.
Improve Xcode Build Speed By 10x (iOS Tips)
5:29
iOS Academy
Переглядів 9 тис.
How to use Lazy in Swift
9:56
Sean Allen
Переглядів 38 тис.