Advanced Swift Generics: Best Solution to Eliminate Code Duplication!

Sdílet
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

Komentáře • 122

  • @malithmnk93
    @malithmnk93 Před 6 lety +2

    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.. ❤️

  • @NikeTao
    @NikeTao Před 6 lety +3

    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!

  • @gjamieostby
    @gjamieostby Před 6 lety +1

    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 Před 5 lety

    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 Před 6 lety +2

    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!

  • @DenisTruong
    @DenisTruong Před 3 lety

    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!

  • @dosbol354
    @dosbol354 Před 6 lety +1

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

  • @ianvisorr
    @ianvisorr Před 6 lety +5

    I liked the video before watch. When the video ended I was going to Like it but it was already liked !
    Thanks Brian! :)

  • @krutomjer
    @krutomjer Před 6 lety +2

    Finally, these are the videos I've been looking for, there are plenty tableview videos, but this semi advanced stuff is awesome.

  • @digitalsuperman
    @digitalsuperman Před 6 lety +2

    this is a good real world sample in using generics.great video bryan.👍🏼👍🏼

  • @namanhvu
    @namanhvu Před 6 lety +1

    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.

  • @brunovavretchek52
    @brunovavretchek52 Před 6 lety +1

    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.👍🏼

  • @isaacclark9825
    @isaacclark9825 Před 6 lety

    Don't know how I missed this great video a few months back. Nicely done.

  • @Dennisvm82
    @Dennisvm82 Před 6 lety

    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.

  • @bezeltipis
    @bezeltipis Před 5 lety +1

    Thanks Brian, i feel my codes are getting better after seeing your video 👍🏻

  • @johniiibibal8838
    @johniiibibal8838 Před 5 lety +3

    * Having a hardtime understanding something *
    * Searches CZcams for "Let's build that app " * - Never fails.
    Thanks Brian!

  • @devanshthesupercool
    @devanshthesupercool Před 5 lety +3

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

  • @ravichandrareddymunagala5654

    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

  • @leopoldromanowski7647
    @leopoldromanowski7647 Před 3 lety

    Wow, this tutorial helped me a lot! You rock LBTA!

  • @nallamillibalaveeraraghava9663

    We used write a custom class for networking . From now I will follow generics awesome keep doing more videos

  • @ZabsonWPR1
    @ZabsonWPR1 Před 4 lety

    Thanks, you helped me with your videos since beginning.

  • @abdouett3676
    @abdouett3676 Před 6 lety +1

    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  Před 6 lety +2

      Indeed, this would be the next step into the refactor process.

  • @ryankanno2562
    @ryankanno2562 Před 3 lety

    This was the coolest video I've ever seen! Thank you!

  • @DesperadoAH
    @DesperadoAH Před 6 lety +1

    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

  • @rafaelrodrigues2595
    @rafaelrodrigues2595 Před 4 lety

    Just what I was looking for! Thanks!

  • @sercanorhangazi
    @sercanorhangazi Před 6 lety

    I wouldn't expect anything less from you! Great video. Again. Thanks!

  • @vinbhai4u
    @vinbhai4u Před 6 lety

    One of the best explanation and use case of generics TY LBTA

  • @TheJhuerta88
    @TheJhuerta88 Před 6 lety

    Great vídeo as usual. Very clear and usefull. Thanks for your work Bryan. I see you have passed 100k subscribers, that's awesome!

  • @JahanJamshidi
    @JahanJamshidi Před 5 lety +1

    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  Před 5 lety

      You would normally add a completion handler paramenter of Error? and complete with an error if any.

    • @JahanJamshidi
      @JahanJamshidi Před 5 lety +1

      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.

  • @JustMills
    @JustMills Před 6 lety

    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.

  • @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

  • @akshanshthakur9235
    @akshanshthakur9235 Před 6 lety

    I love you man. You make my life easier one video at a time

  • @codedeano9585
    @codedeano9585 Před 3 lety

    Woo!! im on a tut that has "Advanced" in the name!!! i must be getting somewhere 🤓🤓

  • @LeachyPeachy99
    @LeachyPeachy99 Před 6 lety

    Great job! Really appreciate the information. I have struggled with Generics so this was very helpful.

  • @stevengao8345
    @stevengao8345 Před 6 lety +1

    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

  • @katoji810
    @katoji810 Před 6 lety

    awesome weekend...thanks you !

  • @HarshitKumar-br3iz
    @HarshitKumar-br3iz Před 5 lety

    Awesome explanation Sir

  • @ajp1653
    @ajp1653 Před 6 lety

    This was some really helpful information! Thank you Brian!

  • @vijaykharage
    @vijaykharage Před 6 lety

    Another amazing tutorial. Thanks Brian!!

  • @christostsangaris4785
    @christostsangaris4785 Před 6 lety +1

    You hear the word "Generics", you see the symbol, you shit your pants, and then comes this guy! Thank you Brian!

  • @faisallikespotatoes
    @faisallikespotatoes Před 6 lety +4

    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] ?

  • @TheZorick
    @TheZorick Před 5 lety +1

    is steins gate in the background ?

  • @_DeProgrammer
    @_DeProgrammer Před 6 lety +1

    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  Před 6 lety +1

      Hey Phillip, the Podcast course goes over how to create a Service class to help you do the refactoring.

  • @barymac5840
    @barymac5840 Před 4 lety

    You're an awesome human being.

  • @Omeir34
    @Omeir34 Před 6 lety +40

    Anyone here who likes the video before even seeing it?

  • @shaktiprakash
    @shaktiprakash Před 6 lety

    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

  • @devjakobsen3595
    @devjakobsen3595 Před 6 lety

    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 😊

  • @ix901x
    @ix901x Před 6 lety

    Awesome Video ! , very helpful

  • @andreypetrov5204
    @andreypetrov5204 Před 3 lety

    Thank you so much!

  • @crazyfox1317
    @crazyfox1317 Před 2 lety

    Thank you!

  • @ombhagwan859
    @ombhagwan859 Před 6 lety

    Awsm video Brian. Can you create generic function for post request if possible please make?

  • @adincebic2992
    @adincebic2992 Před 6 lety

    Very useful video!

  • @Guard-Of-Zion
    @Guard-Of-Zion Před 6 lety

    Yo why only 111k you deserve 1 million bro

  • @magic0x0
    @magic0x0 Před 6 lety

    Great tutorial keep it up pal 👍👍👍

  • @ken6145
    @ken6145 Před 5 lety

    An awesome video. Do you have any courses in which you show us how to apply these skills to iOS app development?

  • @tl2958
    @tl2958 Před 6 lety

    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

  • @adriancervantes1898
    @adriancervantes1898 Před 5 lety

    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!

  • @tharlinhtet97
    @tharlinhtet97 Před 4 lety

    Sir please make a tutorial video of data types such as meta-type, self-type, concrete and opaque types.

  • @botaoli421
    @botaoli421 Před 6 lety

    super clean

  • @bathiyaseneviratne9570

    You're a life saver 🔥

  • @jha8905
    @jha8905 Před 6 lety

    Dude, this was very helpful
    Thanks a lot 🙏🏻😊

  • @zinwinhtet9919
    @zinwinhtet9919 Před 5 lety

    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.

  • @tanggu9510
    @tanggu9510 Před 5 lety

    thank you

  • @82219783
    @82219783 Před 4 lety

    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.

  • @Amazuro
    @Amazuro Před 6 lety

    Awesome, love your vids!

  • @gjermundification
    @gjermundification Před 6 lety

    I see generics as something similar to relationships/constrains in RDBMSs.

  • @bibinjacob123
    @bibinjacob123 Před 6 lety +2

    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  Před 6 lety +1

      Sounds like a good approach, its what UIKit does as well so can't go wrong here.

  • @vakhtangiberidze528
    @vakhtangiberidze528 Před 6 lety

    a very good tip, keep going

  • @ModMyTech
    @ModMyTech Před 6 lety

    Damn that was amazing. Thanks bro!

  • @matthewholder3555
    @matthewholder3555 Před 6 lety

    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.

  • @silver10188
    @silver10188 Před 5 lety

    Great thanks, does T stand for Type?

  • @thakurkrishna7945
    @thakurkrishna7945 Před 6 lety

    Really great video

  • @rohinivaidya3736
    @rohinivaidya3736 Před 4 lety

    Is there a reason to use ObjectMapper libraries to map object? or is the above method of simple model class enough to map data?

  • @MissaMelodica
    @MissaMelodica Před 6 lety

    AWESOME VIDEO!!!

  • @javadarji4654
    @javadarji4654 Před 6 lety

    thank you :)

  • @QiOS283
    @QiOS283 Před 6 lety

    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?

  • @eloh6434
    @eloh6434 Před 6 lety

    Fucking Great Video bro, seriously. Programmatically all the way.

  • @ismailtosun3621
    @ismailtosun3621 Před 5 lety +1

    what about if there are parameters ?

    • @LetsBuildThatApp
      @LetsBuildThatApp  Před 5 lety

      You include the params in your method call or you can add params to your completion block.

  • @northamericantravelerusaca4871

    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

  • @valentine1925
    @valentine1925 Před 6 lety

    I think using (T) -> Void is less confusing than (T) -> (), just a thought tho. thanks btw ^_^

  • @erikaalbizzati7588
    @erikaalbizzati7588 Před 6 lety

    best video =)

  • @alexr3d
    @alexr3d Před 6 lety

    super like for the video ...

  • @parvinderjitsingh5117
    @parvinderjitsingh5117 Před 6 lety

    Hi Brain
    What is your opinion for cross
    Platform like react native or flutter. Will they obsolete the native technologies?

  • @ralmarri12
    @ralmarri12 Před 6 lety

    I wish if you also explain post requests

  • @yashthaker1233
    @yashthaker1233 Před 6 lety

    Awesome

  • @spectre__et
    @spectre__et Před 6 lety

    thanks a lot man

  • @sgomezj_
    @sgomezj_ Před 3 lety

    What if instead of printing it you actually want the value?

  • @Gojam12
    @Gojam12 Před 5 lety

    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  Před 5 lety

      After making videos for 3.5 years, don't be surprised if I neglect a bunch of stuff at this point.

  • @ssdev1652
    @ssdev1652 Před 6 lety

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

  • @antonioalves8306
    @antonioalves8306 Před 6 lety

    really wish you had a package to get all your courses :/

  • @MarkWilliams-es3wc
    @MarkWilliams-es3wc Před 6 lety

    Epic

  • @hectormejia499
    @hectormejia499 Před 6 lety

    hit that like button before even watching the video

  • @ComickOFF
    @ComickOFF Před 5 lety

    Nice. There are mistakes in code...

  • @ix901x
    @ix901x Před 6 lety

    What about Encodable ?

    • @LetsBuildThatApp
      @LetsBuildThatApp  Před 6 lety +1

      What about it?

    • @ix901x
      @ix901x Před 6 lety

      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  Před 6 lety

      Cool, whats not working about this?

    • @ix901x
      @ix901x Před 6 lety

      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  Před 6 lety

      Yeah pretty hard to fix if we don’t know what the problem is

  • @tylerrutt17
    @tylerrutt17 Před 6 lety

    👍👍👍👍🔥🔥

  • @hare2693
    @hare2693 Před 6 lety

    I really hate being told repeatedly how hard something is. Just teach me please. Sheesh.

  • @lingbuqing
    @lingbuqing Před 6 lety

    looks like a chinese