Тёмный

Swift Generics for Beginners - Eliminate Code Duplication 

Sean Allen
Подписаться 151 тыс.
Просмотров 18 тыс.
50% 1

Go to squarespace.com/seanallen to save 10% off your first purchase of a website or domain using code SEANALLEN.
Swift Generics provide a general solution in code that can handle various types. In this video I break down the syntax and use cases for Generics in Swift by going through a number of examples (basic and real life). We talk about protocol conformance and single letter variable names. Then I explain why you should be careful with Generics and not use them everywhere in your code.
This tutorial was created using Xcode 14 and iOS 16.
My iOS Dev Courses:
seanallen.teachable.com/
iOS Developer Interview Questions Playlist:
• Swift Interview Tips
Twitter:
Sean Allen - / seanallen_dev
Hired.com:
hired.com/x/1n01g
Book and learning recommendations that help out the channel if you decide to purchase (Affiliate Links):
Paul Hudson's Hacking With Swift:
gumroad.com/a/762098803
Donny Wals - Combine:
gumroad.com/a/909014131
Mark Moeyken’s SwiftUI Books:
www.bigmountainstudio.com/swiftui-views-book/fzc51
Objc.io Books (Thinking in SwiftUI & Advanced Swift):
gumroad.com/a/656585843
#swift #softwaredeveloper #iosdeveloper
Timestamps:
0:00 - What are Generics?
0:31 - Abstract Example - Code Breakdown
2:08 - Basic Example in Code
4:53 - Generics in Swift Language
6:23 - Real Life Example - Generic Network Call
8:20 - The Generic Balancing Act

Наука

Опубликовано:

 

28 июн 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 27   
@seanallen
@seanallen Год назад
Want to go deeper with Swift and iOS Dev? Check out my iOS Dev courses - seanallen.teachable.com
@danstoian7721
@danstoian7721 Год назад
1:50 So when to use generics as opposed to just "some Drivable"?
@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 😂
@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
@omnikong
@omnikong Год назад
Finally, I understand Generics! Thanks, Sean.
@seanallen
@seanallen Год назад
Happy to hear it!
@ruslangaliev5050
@ruslangaliev5050 Год назад
Thank you for the video! You explain complex things in simple terms!
@seanallen
@seanallen Год назад
Glad you enjoy them, Ruslan!
@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.
@JasonMitchellAZ
@JasonMitchellAZ Год назад
Great explanation and examples
@seanallen
@seanallen Год назад
Thanks Jason!
@bosch5053
@bosch5053 Год назад
Thank you for the video!
@seanallen
@seanallen Год назад
My pleasure!
@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.
@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?
@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 :)
@rkarnes6304
@rkarnes6304 9 месяцев назад
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 3 месяца назад
Which theme are you using in xcode
@seanallen
@seanallen 3 месяца назад
It's called Midnight
@mdtarifkhan1825
@mdtarifkhan1825 9 месяцев назад
❤❤
@seanallen
@seanallen 8 месяцев назад
Glad you liked it!
Далее
How to use Generics in Swift | Advanced Learning #8
19:24
С Анджилишей на тусе💃
00:15
Просмотров 139 тыс.
Swift Optionals - How to Unwrap (real examples)
14:20
Swift Closures Explained
14:23
Просмотров 59 тыс.
async await with API example | Swift Hindi tutorial
22:03
How to use KeyPaths in Swift | Advanced Learning #29
25:12
Generics in Swift: Protocols with associated types
15:37
Advanced Golang: Generics Explained
13:37
Просмотров 56 тыс.
Threads and Grand Central Dispatch in Swift
22:14
Просмотров 21 тыс.
Gizli Apple Watch Özelliği😱
0:14
Просмотров 4,1 млн
#miniphone
0:16
Просмотров 3,6 млн