Swift Generics for Beginners - Eliminate Code Duplication

  Рет қаралды 21,244

Sean Allen

Sean Allen

Күн бұрын

Пікірлер: 28
@seanallen
@seanallen Жыл бұрын
Want to go deeper with Swift and iOS Dev? Check out my iOS Dev courses - seanallen.teachable.com
@Ni7ram
@Ni7ram Жыл бұрын
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 Жыл бұрын
Thanks. I usually get comments about speaking too fast 😂
@omnikong
@omnikong Жыл бұрын
Finally, I understand Generics! Thanks, Sean.
@seanallen
@seanallen Жыл бұрын
Happy to hear it!
@vickywang5049
@vickywang5049 Жыл бұрын
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 Жыл бұрын
1:50 So when to use generics as opposed to just "some Drivable"?
@saeedrahmatolahi462
@saeedrahmatolahi462 Жыл бұрын
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 Жыл бұрын
Thank you for the video! You explain complex things in simple terms!
@seanallen
@seanallen Жыл бұрын
Glad you enjoy them, Ruslan!
@carlosswiftdev2703
@carlosswiftdev2703 Жыл бұрын
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.
@JasonMitchellAZ
@JasonMitchellAZ Жыл бұрын
Great explanation and examples
@seanallen
@seanallen Жыл бұрын
Thanks Jason!
@mipmap256
@mipmap256 5 ай бұрын
One thing I don't understand, since you provide protocol for Generic type, why not just use protocol directly.
@rparham1997
@rparham1997 Жыл бұрын
Can you do a video on protocols?
@seanallen
@seanallen Жыл бұрын
It's on my very long video idea list. So many video ideas, so little time :)
@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.
@yt-1161
@yt-1161 8 ай бұрын
Which theme are you using in xcode
@seanallen
@seanallen 8 ай бұрын
It's called Midnight
@bosch5053
@bosch5053 Жыл бұрын
Thank you for the video!
@seanallen
@seanallen Жыл бұрын
My pleasure!
@faruuqqayyum3811
@faruuqqayyum3811 Жыл бұрын
Can you do a video for "Opaque Types"?
@seanallen
@seanallen Жыл бұрын
I'll add it to the idea list. However that list is VERY long :)
@mdtarifkhan1825
@mdtarifkhan1825 Жыл бұрын
❤❤
@seanallen
@seanallen Жыл бұрын
Glad you liked it!
Swift Dependency Injection - What is it? What are the benefits?
6:58
Swift Closures Explained
14:23
Sean Allen
Рет қаралды 68 М.
The Ultimate Sausage Prank! Watch Their Reactions 😂🌭 #Unexpected
00:17
La La Life Shorts
Рет қаралды 9 МЛН
كم بصير عمركم عام ٢٠٢٥😍 #shorts #hasanandnour
00:27
hasan and nour shorts
Рет қаралды 11 МЛН
За кого болели?😂
00:18
МЯТНАЯ ФАНТА
Рет қаралды 3,3 МЛН
Swift API Calls for Beginners (Networking) - Async Await & JSON
25:35
Cleaner, Safer Code with Swift KeyPaths
7:22
Sean Allen
Рет қаралды 10 М.
Each Media Query in CSS You MUST Know
4:47
IFace
Рет қаралды 848
Swift Basics: Array vs. Set
9:32
Sean Allen
Рет қаралды 20 М.
31 Xcode Tips & Tricks - 2023
17:45
Sean Allen
Рет қаралды 42 М.
Data Flow in a SwiftUI App
33:15
CodeWithChris
Рет қаралды 54 М.
How to use Lazy in Swift
9:56
Sean Allen
Рет қаралды 39 М.