Type Erasure In Swift | iOS Development

preview_player
Показать описание
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!

** Popular Series

** Get Skillshare free for 2 Months and learn iOS

** Manage all your investments from app earnings on Betterment!

** Grow your own Youtube tech channel with TubeBuddy:

#swift #iOSDeveloper #Xcode
Рекомендации по теме
Комментарии
Автор

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).

nX-
Автор

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

Stricken
Автор

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?

aecollector
Автор

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

gribovmax
Автор

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

hackenbacker
Автор

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

Landon_Hughes
Автор

Is this video about VIPER Architecture?

shinitiomit
Автор

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

dmitriymazurenko
Автор

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

artemsemavin