Advanced Swift Generics: Best Solution to Eliminate Code Duplication!

  Рет қаралды 38,606

Lets Build That App

Lets Build That App

6 жыл бұрын

Copying and pasting code often occurs in our application when we perform generic code logic. This is especially apparent with networking code. I'd like to share with you how you can use Generics to eliminate a lot of code duplication in your codebase. Enjoy.
Podcasts Course
www.letsbuildthatapp.com/cour...
Intermediate Training Core Data
www.letsbuildthatapp.com/cour...
Instagram Firebase Course
www.letsbuildthatapp.com/cour...
Facebook Group
/ 1240636442694543
Completed Source Code
www.letsbuildthatapp.com/cour...
Instagram: / buildthatapp
Twitter: / buildthatapp
Hackintosh Build
i9 9900k: amzn.to/2CvQj6v
ASUS Z390 Motherboard: amzn.to/2JpaQ2B
16GB Corsair Vengeance RGB RAM: amzn.to/2YbygvJ
Corsair H100i Platinum Water Cooler: amzn.to/2FnbM22
Thermaltake RGB Power Supply: amzn.to/2TgKDTu
Intel 1TB SSD: amzn.to/2Tkdk1V
Corsair 280x Computer Case: amzn.to/2WcjvH2
AMD Radeon 4GB RAM GFX Card: amzn.to/2TYMyAp
LG 34" Widescreen 3440 x 1440: amzn.to/2Tk7q0S
Rode NT1 Microphone: amzn.to/2TZgX1r
Focusrite Audio Interface: amzn.to/2U12gey
Panasonic GH4 DSLR: amzn.to/2U4BrG6
Magewell HDMI to USB3 Capture Card: amzn.to/2Jp2esI

Пікірлер: 122
@krutomjer
@krutomjer 6 жыл бұрын
Finally, these are the videos I've been looking for, there are plenty tableview videos, but this semi advanced stuff is awesome.
@namanhvu
@namanhvu 6 жыл бұрын
This was very informative. I feel confident in attempting to apply this to your Instagram Firebase course. Please do keep the Swift content coming as much as possible. Cheers, Brian.
@Dennisvm82
@Dennisvm82 6 жыл бұрын
Very nice, Brian! Love the idea of using generics to make everything so much more elegant and professional. Hard topic to actually get used to, but once you start using them it all makes so much more sense. They kind of feel like more advanced closures.
@ianvisorr
@ianvisorr 6 жыл бұрын
I liked the video before watch. When the video ended I was going to Like it but it was already liked ! Thanks Brian! :)
@NikeTao
@NikeTao 6 жыл бұрын
This is fantastic! I've been looking for this a year ago and actually tried using generics, but didn't manage to implement them into a network call. Thanks, networking will never be the same!
@malithmnk93
@malithmnk93 6 жыл бұрын
Thanx Brian as always.. Now I am working as a ios developer since 8 month because of you. When I start to follow your tutorial. I didn't know anything about swift or ios development. but I loved ios development and I show good job market in that field. Then I followed you. Now I work as ios developer here in Sri Lanka because of you. Thank you Brian, thank again.. ❤️
@gjamieostby
@gjamieostby 5 жыл бұрын
I just started coding about a year ago as a hobby. I’v taken some courses online via Udemy and found som other sources as well as on youtube. I find them to be very slow paced and difficult to follow for that reason and how they in general are very limiting in respect to what you learn to do. That beeing said, I really have to say that your videos are so far ahead on all the topics that a beginner coder really want to learn so to be able to evolve! The fast pace in combination with how clear you explain things, make these videos that much easier to follow and pleasant. Your quality and apparent knowledge is truly inspiring. So thank you! The best stuff online, you should get an award!
@Holphana
@Holphana 5 жыл бұрын
Out of all the free swift videos I have used to learn you are undoubtedly the most professional and provide real top notch quality videos, please don’t ever stop.
@BombheadGuitar
@BombheadGuitar 6 жыл бұрын
Great video Brian, and perfectly timed. I have a class with 6 or 7 of these, and although I created a "FetchData" function to handle all the calls, I still had a function for each type of call. My code will be refactored ASAP to include generics. Thanks!
@isaacclark9825
@isaacclark9825 5 жыл бұрын
Don't know how I missed this great video a few months back. Nicely done.
@DesperadoAH
@DesperadoAH 6 жыл бұрын
Thank you for all the work you do to teach us how to be better on this .. I have learned a lot form your tutorials
@digitalsuperman
@digitalsuperman 6 жыл бұрын
this is a good real world sample in using generics.great video bryan.👍🏼👍🏼
@TheJhuerta88
@TheJhuerta88 6 жыл бұрын
Great vídeo as usual. Very clear and usefull. Thanks for your work Bryan. I see you have passed 100k subscribers, that's awesome!
@brunovavretchek52
@brunovavretchek52 6 жыл бұрын
You know... My name’s Bruno I’m from Brazil and I learn a lot watching your videos! I would like to watch another series where you clone app’s like Twitch TV, Facebook and etc. What I mean is that I learn so much when you upload videos like that, because I create the app literally with you! Well, here is my suggestion for future videos. I hope you agree with me! lol Thank you so much for being this awesome guy.👍🏼
@sercanorhangazi
@sercanorhangazi 6 жыл бұрын
I wouldn't expect anything less from you! Great video. Again. Thanks!
@bezeltipis
@bezeltipis 5 жыл бұрын
Thanks Brian, i feel my codes are getting better after seeing your video 👍🏻
@LeachyPeachy99
@LeachyPeachy99 6 жыл бұрын
Great job! Really appreciate the information. I have struggled with Generics so this was very helpful.
@nallamillibalaveeraraghava9663
@nallamillibalaveeraraghava9663 6 жыл бұрын
We used write a custom class for networking . From now I will follow generics awesome keep doing more videos
@akshanshthakur9235
@akshanshthakur9235 6 жыл бұрын
I love you man. You make my life easier one video at a time
@devanshthesupercool
@devanshthesupercool 5 жыл бұрын
You rock dude you sparked the interest of iOS in me to the point I am an apple fan boy now xD love these advanced stuff because code optimization is what defines good code :)
@leopoldromanowski7647
@leopoldromanowski7647 3 жыл бұрын
Wow, this tutorial helped me a lot! You rock LBTA!
@ZabsonWPR1
@ZabsonWPR1 4 жыл бұрын
Thanks, you helped me with your videos since beginning.
@ryankanno2562
@ryankanno2562 3 жыл бұрын
This was the coolest video I've ever seen! Thank you!
@ajp1653
@ajp1653 6 жыл бұрын
This was some really helpful information! Thank you Brian!
@rafaelrodrigues2595
@rafaelrodrigues2595 4 жыл бұрын
Just what I was looking for! Thanks!
@vijaykharage
@vijaykharage 6 жыл бұрын
Another amazing tutorial. Thanks Brian!!
@vinbhai4u
@vinbhai4u 6 жыл бұрын
One of the best explanation and use case of generics TY LBTA
@DenisTruong
@DenisTruong 3 жыл бұрын
started learning seriously a month ago, it would have been hard to follow this video, but I was able to just hang on, great video on generics Thank you!
@ravichandrareddymunagala5654
@ravichandrareddymunagala5654 5 жыл бұрын
Amazing topic. This topic can be reduce lot of code and it can be helpful increase app performence and time reduce. Thank you very much for the introduce
@basil5060
@basil5060 6 жыл бұрын
Awesome Video ! , very helpful
@Amazuro
@Amazuro 6 жыл бұрын
Awesome, love your vids!
@jha8905
@jha8905 6 жыл бұрын
Dude, this was very helpful Thanks a lot 🙏🏻😊
@HarshitKumar-br3iz
@HarshitKumar-br3iz 5 жыл бұрын
Awesome explanation Sir
@dosbol354
@dosbol354 6 жыл бұрын
THANK YOU VERY MUCH, I've been searching how to reduce amount of useless code for a whole week. I've got 5 almost the same functions with different object that has to be decoded, but I didn't how to implement everything in one function. Thank you again, now I'm thinking where to put this code(Model, Controller or separate Network Layer?)
@adincebic2992
@adincebic2992 6 жыл бұрын
Very useful video!
@barymac5840
@barymac5840 4 жыл бұрын
You're an awesome human being.
@magic0x0
@magic0x0 6 жыл бұрын
Great tutorial keep it up pal 👍👍👍
@bathiyaseneviratne9570
@bathiyaseneviratne9570 3 жыл бұрын
You're a life saver 🔥
@abdouett3676
@abdouett3676 6 жыл бұрын
Great work. Thanks! It's possible to make generic function more powerful by creating a API endpoints enum and add a parameters parameter to handle different URLS.
@LetsBuildThatApp
@LetsBuildThatApp 6 жыл бұрын
Indeed, this would be the next step into the refactor process.
@andreypetrov5204
@andreypetrov5204 3 жыл бұрын
Thank you so much!
@zinwinhtet9919
@zinwinhtet9919 5 жыл бұрын
This is very awesome one.. I'm just looking for such kinda idea and your video saved me. Thz.. Btw, I'm using Alamofire tho.
@johniiibibal8838
@johniiibibal8838 5 жыл бұрын
* Having a hardtime understanding something * * Searches KZfaq for "Let's build that app " * - Never fails. Thanks Brian!
@katoji810
@katoji810 6 жыл бұрын
awesome weekend...thanks you !
@JustMills
@JustMills 6 жыл бұрын
Thank you, I've tried reading about genetics but never really got it (I learn better through video and demos). I think I understand it enough to be able to apply this with Java. I'll have to do some more reason but having a basic understanding really helps.
@Omeir34
@Omeir34 6 жыл бұрын
Anyone here who likes the video before even seeing it?
@dilsmatchanov
@dilsmatchanov 6 жыл бұрын
Me )
@tylerrutt17
@tylerrutt17 6 жыл бұрын
Yep!
@paweambrozej2667
@paweambrozej2667 6 жыл бұрын
I've been waiting for that video since the video about "code vs storyboard". Love it !
@dilsmatchanov
@dilsmatchanov 6 жыл бұрын
It was a long period of time... You could find a video or a tutorial about generics somewhere else
@user-ry6ix2mr8y
@user-ry6ix2mr8y 6 жыл бұрын
+))
@crazyfox1317
@crazyfox1317 2 жыл бұрын
Thank you!
@ModMyTech
@ModMyTech 6 жыл бұрын
Damn that was amazing. Thanks bro!
@vakhtangiberidze528
@vakhtangiberidze528 6 жыл бұрын
a very good tip, keep going
@Guard-Of-Zion
@Guard-Of-Zion 6 жыл бұрын
Yo why only 111k you deserve 1 million bro
@MissaMelodica
@MissaMelodica 6 жыл бұрын
AWESOME VIDEO!!!
@botaoli421
@botaoli421 6 жыл бұрын
super clean
@thakurkrishna7945
@thakurkrishna7945 6 жыл бұрын
Really great video
@stevengao8345
@stevengao8345 6 жыл бұрын
great tutorial, whenever u have time would you talk about the completion handler with @escaping , why and how the value is retained, and how long the value will be retained inside the closure. this topic still confuse me, and it is super common these days. you did mentioned this topic in some of your videos , but never really get in to detail, wutever you got time, thanks
@adriancervantes1898
@adriancervantes1898 5 жыл бұрын
this is really awesome!, thanks Brian, one question... how I can set a function like this but using headers and params, like a POST or GET? I hope one day you can iluminate our minds using that kind of petitions. thanks again!
@shaktiprakash
@shaktiprakash 6 жыл бұрын
Nice tutorial ,it would be awesome if you make a video related to decodable with coding keys related stuffs,like in response for any parameters the data type maybe string or int ,how to handle that
@ombhagwan859
@ombhagwan859 6 жыл бұрын
Awsm video Brian. Can you create generic function for post request if possible please make?
@_DeProgrammer
@_DeProgrammer 6 жыл бұрын
Brian, You're the man. Was trying to wrap my head around this last night. Having trouble refactoring my network call into it's own file. Would I use delegation to make a network call and turn the data into a struct to use in my viewController? Do you have any videos or any good resources on this topic you can point me towards? thanks for this video it really helped!
@LetsBuildThatApp
@LetsBuildThatApp 6 жыл бұрын
Hey Phillip, the Podcast course goes over how to create a Service class to help you do the refactoring.
@christostsangaris4785
@christostsangaris4785 6 жыл бұрын
You hear the word "Generics", you see the symbol, you shit your pants, and then comes this guy! Thank you Brian!
@spectre__et
@spectre__et 5 жыл бұрын
thanks a lot man
@faisallikespotatoes
@faisallikespotatoes 6 жыл бұрын
Great video as always Brian! Quick question - is it safer to use the JSONDecodable to load our model data from JSON, or to write initializers for our models that take in a JSON object type of [String : Any] ?
@godwinolorunshola7913
@godwinolorunshola7913 6 жыл бұрын
You should make use of decodable.
@Cdswjp
@Cdswjp 5 жыл бұрын
Why would there be a difference
@tanggu9510
@tanggu9510 5 жыл бұрын
thank you
@codedeano9585
@codedeano9585 3 жыл бұрын
Woo!! im on a tut that has "Advanced" in the name!!! i must be getting somewhere 🤓🤓
@tl2958
@tl2958 6 жыл бұрын
Hi, thanks for your channel, we can learn a lot ! This video is very good but can you explain how handle error properly to inform the user ? I try several tweaks on your code but I'm a beginner :) Can we use the completion to return the error (from data task, or bad json) Thanks in advance from Paris, France
@82219783
@82219783 4 жыл бұрын
wow thanks for this!!! have been searching how to do this for the whole day. but I have to pass the data back to my viewcontroller as I have separated out the api manger and the viewcontroller and wondering is there a good way to do this? right now I'm using codable as the datatype for the api data struct to be passed into the delegate methods. just wondering how you would do it.
@alexr3d
@alexr3d 6 жыл бұрын
super like for the video ...
@ken6145
@ken6145 5 жыл бұрын
An awesome video. Do you have any courses in which you show us how to apply these skills to iOS app development?
@javadarji4654
@javadarji4654 6 жыл бұрын
thank you :)
@erikaalbizzati7588
@erikaalbizzati7588 6 жыл бұрын
best video =)
@silver10188
@silver10188 5 жыл бұрын
Great thanks, does T stand for Type?
@JahanJamshidi
@JahanJamshidi 5 жыл бұрын
Question: Hi, I really enjoyed this session... very useful, I use this option in multiple apps. in the function that calls the fetchgenric, how do I check for the "return"s. for example if the response status code is not 200, how do I know it's not. right now if the completion handler is not called during one of the "return"s in the fetchgeneric function, my calling function just waits and nothing happens. thank you again for such a great channel you have ... have a great day.
@LetsBuildThatApp
@LetsBuildThatApp 5 жыл бұрын
You would normally add a completion handler paramenter of Error? and complete with an error if any.
@JahanJamshidi
@JahanJamshidi 5 жыл бұрын
makes sense. I was having a hard time sending a second parameter back to the calling function. but after spending a few hours, I figure it out. I realized returning in some cases causes the threads to be messed up ... so returning errors should always be on the main thread ... Thank you again for your help.
@yashthaker1233
@yashthaker1233 6 жыл бұрын
Awesome
@brunolopesdemello7040
@brunolopesdemello7040 Жыл бұрын
To me generics ways was the hardest part about programming, because sometimes it's difficult to identity whenever you can use it and best approach for it
@bibinjacob123
@bibinjacob123 6 жыл бұрын
Hey Brian, This is really good. But, i've been working for a while on completion block in functions. Found that if the request is getting an error completion block is not being executed. So, I added the error and optional generic object in completion. Do you have a better approach?
@LetsBuildThatApp
@LetsBuildThatApp 6 жыл бұрын
Sounds like a good approach, its what UIKit does as well so can't go wrong here.
@rohinivaidya3736
@rohinivaidya3736 4 жыл бұрын
Is there a reason to use ObjectMapper libraries to map object? or is the above method of simple model class enough to map data?
@matthewholder3555
@matthewholder3555 6 жыл бұрын
Hey Brian, have you ever considered freelancing iOS. Like working with other clients. What was your reason for doing education for iOS ? I was thinking about freelancing but not too sure.
@jyots4283
@jyots4283 6 жыл бұрын
I have 3 screens that contain almost the same display for UITableView and cells. All the three screen have same table structure but model type is different for all screens. I can do that but I have to repeat the same code for tableview in all screens: (cellforRow, Number, data, and calculations...) what is the best approach to avoid duplicating data in such a case? Should I create parent UITabledatasource or UITableView?
@parvinderjitsingh5117
@parvinderjitsingh5117 6 жыл бұрын
Hi Brain What is your opinion for cross Platform like react native or flutter. Will they obsolete the native technologies?
@TheZorick
@TheZorick 5 жыл бұрын
is steins gate in the background ?
@eloh6434
@eloh6434 6 жыл бұрын
Fucking Great Video bro, seriously. Programmatically all the way.
@devjakobsen3595
@devjakobsen3595 6 жыл бұрын
Have you tried out swagger and react? You’re able to get all services/APIs generated for you. Just an alternative from genetics to code less of the same code in the network layer over and over again 😊
@gjermundification
@gjermundification 6 жыл бұрын
I see generics as something similar to relationships/constrains in RDBMSs.
@tharlinhtet97
@tharlinhtet97 4 жыл бұрын
Sir please make a tutorial video of data types such as meta-type, self-type, concrete and opaque types.
@northamericantravelerusaca4871
@northamericantravelerusaca4871 6 жыл бұрын
Hi Brian , I have a request for a video :), Would you please make a video about ReplayKit, I need to add this feature to my App but it's not working after IOS 11 and all existing videos are for before IOS 11 (2017) Thanks in advance Pooya
@ssdev1652
@ssdev1652 6 жыл бұрын
hey, I'm trying to modify this func to support enum completion error handling, for example: enum Result { case success(T) case failure(String) } and I'm getting Result in completion when calling a function and "Generic parameter 'T' could not be inferred" error. I'm stuck.. if you could help me i would be super grateful :)
@MarkWilliams-es3wc
@MarkWilliams-es3wc 6 жыл бұрын
Epic
@tylerrutt17
@tylerrutt17 6 жыл бұрын
👍👍👍👍🔥🔥
@Gojam12
@Gojam12 5 жыл бұрын
After your catch in fetchDetails shouldn't you have used this method print(" Failed to decode json \(jsonErr "). This video is not that old and your method is not working in my code?
@LetsBuildThatApp
@LetsBuildThatApp 5 жыл бұрын
After making videos for 3.5 years, don't be surprised if I neglect a bunch of stuff at this point.
@ralmarri12
@ralmarri12 6 жыл бұрын
I wish if you also explain post requests
@valentine1925
@valentine1925 6 жыл бұрын
I think using (T) -> Void is less confusing than (T) -> (), just a thought tho. thanks btw ^_^
@antonioalves8306
@antonioalves8306 6 жыл бұрын
really wish you had a package to get all your courses :/
@ismailtosun3621
@ismailtosun3621 5 жыл бұрын
what about if there are parameters ?
@LetsBuildThatApp
@LetsBuildThatApp 5 жыл бұрын
You include the params in your method call or you can add params to your completion block.
@sgomezj_
@sgomezj_ 3 жыл бұрын
What if instead of printing it you actually want the value?
@hectormejia499
@hectormejia499 6 жыл бұрын
hit that like button before even watching the video
@basil5060
@basil5060 6 жыл бұрын
What about Encodable ?
@LetsBuildThatApp
@LetsBuildThatApp 6 жыл бұрын
What about it?
@basil5060
@basil5060 6 жыл бұрын
I tried to make new function using Encodable but I think I forget something or do something wrong fileprivate func postGenericData(urlString: String,parameter: T, completion: @escaping (T) -> ()) { let url = URL(string: urlString) var request = URLRequest(url: url!) request.httpMethod = "POST" request.addValue("application/json", forHTTPHeaderField: "Content-Type") URLSession.shared.dataTask(with: url!) { (data, resp, err) in if let err = err { print("Failed to post data:", err) return } guard let data = data else { return } do { let jsonBody = try JSONEncoder().encode(parameter) request.httpBody = jsonBody completion(parameter) } catch let jsonErr { print("Failed to encode json:", jsonErr) } }.resume() } }
@LetsBuildThatApp
@LetsBuildThatApp 6 жыл бұрын
Cool, whats not working about this?
@basil5060
@basil5060 6 жыл бұрын
I got statusCode: 405 , I did tried the same thing on Postman and the statusCode was 200 . I didn't know what is the problem
@LetsBuildThatApp
@LetsBuildThatApp 6 жыл бұрын
Yeah pretty hard to fix if we don’t know what the problem is
@ducnguyendinhtrung4745
@ducnguyendinhtrung4745 2 жыл бұрын
11:55 13:46
@ComickOFF
@ComickOFF 4 жыл бұрын
Nice. There are mistakes in code...
@lingbuqing
@lingbuqing 6 жыл бұрын
looks like a chinese
@hare2693
@hare2693 6 жыл бұрын
I really hate being told repeatedly how hard something is. Just teach me please. Sheesh.
Swift Animations: Facebook Shimmer using Gradient Masks
20:44
Lets Build That App
Рет қаралды 43 М.
Swift Generics: The Key to Reusable and Flexible Code
23:47
Swift and Tips
Рет қаралды 6 М.
Каха и суп
00:39
К-Media
Рет қаралды 4,2 МЛН
Final muy increíble 😱
00:46
Juan De Dios Pantoja 2
Рет қаралды 54 МЛН
Did you believe it was real? #tiktok
00:25
Анастасия Тарасова
Рет қаралды 53 МЛН
Мы никогда не были так напуганы!
00:15
Аришнев
Рет қаралды 6 МЛН
Generic Swift: It Isn’t Supposed to Hurt - Rob Napier
58:16
ChariotSolutions
Рет қаралды 7 М.
Loops and Hash Maps Job Preparation Interview Question
15:55
Lets Build That App
Рет қаралды 20 М.
The EASIEST Swift Generics Tutorial 2022 🔥
6:06
Indently
Рет қаралды 2,4 М.
When to Semaphore vs Dispatch Group! Careful Multithreaded Shared Resource
11:11
How to use Generics in Swift | Advanced Learning #8
19:24
Swiftful Thinking
Рет қаралды 14 М.
Weak and Unowned Self Closure Memory Leak Fixes
12:21
Lets Build That App
Рет қаралды 56 М.
JAVA DTO Pattern Tutorial | Simplify Your Code
19:12
Amigoscode
Рет қаралды 193 М.
Generics in Swift: Where Clauses (and something else!)
20:10
Swift and Tips
Рет қаралды 2,5 М.
Advanced Swift: Making Programmatic Auto Layout Easy through Extensions
25:38
№37 - Generics в Swift | Прошел этого босса прошел Swift!
1:47:57
В’ячеслав Білий
Рет қаралды 8 М.
Каха и суп
00:39
К-Media
Рет қаралды 4,2 МЛН