Advanced Swift Generics: Best Solution to Eliminate Code Duplication!
Vložit
- čas přidán 7. 09. 2024
- 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.letsbuildt...
Intermediate Training Core Data
www.letsbuildt...
Instagram Firebase Course
www.letsbuildt...
Facebook Group
/ 1240636442694543
Completed Source Code
www.letsbuildt...
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
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.. ❤️
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!
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!
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.
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!
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!
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?)
I liked the video before watch. When the video ended I was going to Like it but it was already liked !
Thanks Brian! :)
Finally, these are the videos I've been looking for, there are plenty tableview videos, but this semi advanced stuff is awesome.
this is a good real world sample in using generics.great video bryan.👍🏼👍🏼
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.
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.👍🏼
Don't know how I missed this great video a few months back. Nicely done.
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.
Thanks Brian, i feel my codes are getting better after seeing your video 👍🏻
* Having a hardtime understanding something *
* Searches CZcams for "Let's build that app " * - Never fails.
Thanks Brian!
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 :)
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
Wow, this tutorial helped me a lot! You rock LBTA!
We used write a custom class for networking . From now I will follow generics awesome keep doing more videos
Thanks, you helped me with your videos since beginning.
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.
Indeed, this would be the next step into the refactor process.
This was the coolest video I've ever seen! Thank you!
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
Just what I was looking for! Thanks!
I wouldn't expect anything less from you! Great video. Again. Thanks!
One of the best explanation and use case of generics TY LBTA
Great vídeo as usual. Very clear and usefull. Thanks for your work Bryan. I see you have passed 100k subscribers, that's awesome!
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.
You would normally add a completion handler paramenter of Error? and complete with an error if any.
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.
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.
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
I love you man. You make my life easier one video at a time
Woo!! im on a tut that has "Advanced" in the name!!! i must be getting somewhere 🤓🤓
Great job! Really appreciate the information. I have struggled with Generics so this was very helpful.
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
awesome weekend...thanks you !
Awesome explanation Sir
This was some really helpful information! Thank you Brian!
Another amazing tutorial. Thanks Brian!!
You hear the word "Generics", you see the symbol, you shit your pants, and then comes this guy! Thank you Brian!
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] ?
You should make use of decodable.
Why would there be a difference
is steins gate in the background ?
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!
Hey Phillip, the Podcast course goes over how to create a Service class to help you do the refactoring.
You're an awesome human being.
Anyone here who likes the video before even seeing it?
Me )
Yep!
I've been waiting for that video since the video about "code vs storyboard". Love it !
It was a long period of time... You could find a video or a tutorial about generics somewhere else
+))
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
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 😊
Awesome Video ! , very helpful
Thank you so much!
Thank you!
Awsm video Brian. Can you create generic function for post request if possible please make?
Very useful video!
Yo why only 111k you deserve 1 million bro
Great tutorial keep it up pal 👍👍👍
An awesome video. Do you have any courses in which you show us how to apply these skills to iOS app development?
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
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!
Sir please make a tutorial video of data types such as meta-type, self-type, concrete and opaque types.
super clean
You're a life saver 🔥
Dude, this was very helpful
Thanks a lot 🙏🏻😊
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.
thank you
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.
Awesome, love your vids!
I see generics as something similar to relationships/constrains in RDBMSs.
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?
Sounds like a good approach, its what UIKit does as well so can't go wrong here.
a very good tip, keep going
Damn that was amazing. Thanks bro!
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.
Great thanks, does T stand for Type?
Really great video
Is there a reason to use ObjectMapper libraries to map object? or is the above method of simple model class enough to map data?
AWESOME VIDEO!!!
thank you :)
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?
Fucking Great Video bro, seriously. Programmatically all the way.
what about if there are parameters ?
You include the params in your method call or you can add params to your completion block.
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
I think using (T) -> Void is less confusing than (T) -> (), just a thought tho. thanks btw ^_^
best video =)
super like for the video ...
Hi Brain
What is your opinion for cross
Platform like react native or flutter. Will they obsolete the native technologies?
I wish if you also explain post requests
Awesome
thanks a lot man
What if instead of printing it you actually want the value?
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?
After making videos for 3.5 years, don't be surprised if I neglect a bunch of stuff at this point.
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 :)
really wish you had a package to get all your courses :/
Epic
hit that like button before even watching the video
Nice. There are mistakes in code...
What about Encodable ?
What about it?
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()
}
}
Cool, whats not working about this?
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
Yeah pretty hard to fix if we don’t know what the problem is
11:55 13:46
👍👍👍👍🔥🔥
I really hate being told repeatedly how hard something is. Just teach me please. Sheesh.
looks like a chinese