Swift 5 Brand New Result Type: Write Cleaner API code

Поділитися
Вставка
  • Опубліковано 18 гру 2024

КОМЕНТАРІ • 117

  • @LohenYumnam
    @LohenYumnam 5 років тому +2

    This video is by far the most mind clearing tutorial on Swift Result type. good job Brian.

  • @rajanmaheshwari
    @rajanmaheshwari 5 років тому +18

    Hi Brian. This is now introduced in SDK but this concept was already present in SwiftyJSON and Moya from a very long time. Also we can do it in 4.2 too

  • @christostsangaris4785
    @christostsangaris4785 5 років тому +1

    This is a nice touch! By the way, just go and purchase all of Brian's courses and thank me later! Wish I had Brian to guide me when I was starting Swift development. But its never too late!

  • @kama8213
    @kama8213 4 роки тому

    Number one favorite tutor!

  • @saschasimon812
    @saschasimon812 5 років тому

    I did not know abouzt this feature, but it will make my callbacks a lot easier and more readable - thank you!

  • @alondob
    @alondob 5 років тому

    Glad to see this make it into Swift.

  • @robertotomas
    @robertotomas 5 років тому +1

    You, sir, get mad kudos for this

  • @t19960804
    @t19960804 5 років тому

    It's very useful video!!!
    I was confused about how to use "Associate Value" before
    Now It's all clear
    thank you so much Brian

  • @awais.fayyaz
    @awais.fayyaz 5 років тому

    Hey Brian!, what a nice tutorial. Indeed a practical example clarifies the concept very clearly.
    Thanks for the video and keep Up!

  • @badakml7366
    @badakml7366 5 років тому

    thanks man, it makes my alamofire implementation more cleaner

  • @subratglamindia
    @subratglamindia 5 років тому

    Hi Brian. , this is very good info .I have a problem to support iPad Pro(12.9 inch) screen. I added different font for iPhone and iPad. its working properly in other devices but in iPad Pro(12.9 inch) screen the text is too small and unable to read. need your help on this

  • @Ipadstands
    @Ipadstands 5 років тому

    It is just fun to watch your videos . It will be a few months before I can/would use this.

  • @krutomjer
    @krutomjer 5 років тому

    Very nice, I’ll implement this tomorrow since I’m refactoring some network code. Thanks Brian

  • @olmosbekkhurramov5259
    @olmosbekkhurramov5259 3 роки тому

    Thank you Brain! This video help me

  • @BLT-Tech
    @BLT-Tech 4 роки тому

    nice video Brian. I got this working to fetch data & update my storyboard labels during viewDidLoad. My question is this... my URL string is 'http...(myhost)...?channel=' and I append the channel# when I send it. My API returns different data sets (same struc) for each channel, so after viewDidLoad channel=1, I would like to re-hit the API with channel 2 after 5secs, then channel 3, etc. How do I do this? I tried looping in viewDidLoad but it only updated my labels the last hit. Thanks :)

  • @WinstonSalcedo
    @WinstonSalcedo 5 років тому

    Thank you Brian for this tutorial.

  • @ScalpersCorner
    @ScalpersCorner 5 років тому

    why does moving .self to the outside of the bracket return the entire array? @ 5:56 a little confused on how .self is operating in this scenario. (new to swift)

    • @LetsBuildThatApp
      @LetsBuildThatApp  5 років тому

      That's just how it works, there's no explanation beyond that. You just have to look at more examples at parsing JSON objects. For more lessons on JSON, please visit these episodes: www.letsbuildthatapp.com/course_video?id=4662

    • @ScalpersCorner
      @ScalpersCorner 5 років тому

      @@LetsBuildThatApp Will do, thanks for the reply, you are an amazing teacher!

  • @ГеннадийОлейник-ч4в
    @ГеннадийОлейник-ч4в 5 років тому +2

    Hi Brain! Nice tutorial!
    As for me, I prefer to make an API with separate success and failure closures. Is there something that I may lost with it?

    • @LetsBuildThatApp
      @LetsBuildThatApp  5 років тому +3

      Yes, if you're writing a lot of APIs then it becomes tedious typing out both closures. You'll get used to this concept quick enough when other APIs adopt this syntax.

  • @dany3370
    @dany3370 5 років тому +5

    Even though i switched to Web development , still following Brian for the vids

    • @vkray
      @vkray 5 років тому

      r u using golang for web development?

    • @dany3370
      @dany3370 5 років тому

      @@vkray yeah

    • @thegeeksides
      @thegeeksides 5 років тому

      Are you doing fullstack or just backend?

    • @dany3370
      @dany3370 5 років тому

      @@thegeeksides fullstack

  • @tieda
    @tieda 5 років тому

    Waiting for this! :)

  • @ridhomuhammad17
    @ridhomuhammad17 5 років тому +3

    hi ..brain, I interest with your setup hackintosh , can u make video about that....thanks

    • @baloiradu7460
      @baloiradu7460 4 роки тому

      if you want a smaller better looking mac mini go for nuc8i5bek or nuc10i5

  • @ProductsHub365
    @ProductsHub365 3 роки тому

    What if we have category key with course name inside each array dict object under array and want to display list of objects based on category using segment selection in single tableview controller ? How can we achieve this ?

  • @Jaesleeshoots
    @Jaesleeshoots 5 років тому

    Great video man

  • @Zainanjum100
    @Zainanjum100 4 роки тому

    We can make a generic typealias Completion = (Result) -> ()
    and later we can simply use completion: @escaping Completion
    if we want to make only one function for decoding and api response

  • @donathmm3881
    @donathmm3881 5 років тому

    Thank you for that great tutorial

  • @jasondhindsa6774
    @jasondhindsa6774 4 роки тому

    Is it possible to configure Result so that it includes more than just success and error? What if you need a few success objects?

  • @hiteshrasal5222
    @hiteshrasal5222 5 років тому

    Hi nice tutorial..can you please made tutorial on clean architecture with API request example?

  • @Nightsd01
    @Nightsd01 5 років тому

    This is funny - I’ve been doing this since Swift 2 with generics, I’ve just been building the Result type myself.

  • @TheMcGloneCode
    @TheMcGloneCode 5 років тому

    My only problem with the Result approach, which Alamofire uses, is when I want data and an error.
    For example, Instagram’s API sets the http status code AND returns JSON. I still want that JSON.

  • @sourabhgajbhiye3574
    @sourabhgajbhiye3574 5 років тому

    Awesome tutorial 😎

  • @peterirving9458
    @peterirving9458 5 років тому

    Hey Brian, Thanks for this tutorial. I have a question for you.
    I am curious if you have any information on handling the "response" var from the URL session completion. I assume response is the http error, like 200s for success, 400s for client error, etc. Is there much use to handling this response? I am currently building a simple backend with flask and I can return a json and a response code back with each route if I want. Are http responses necessary for mobile development?

    • @LetsBuildThatApp
      @LetsBuildThatApp  5 років тому

      You simply check the status code of the response for errors, just like any other error.

  • @charlesm.1638
    @charlesm.1638 4 роки тому

    Can I call this from a class located in another Swift File? Trying to focus on OOP in Swift.

  • @VertoSH
    @VertoSH 5 років тому +5

    Oh so they made "native" the result framework we had to import to use alamofire and Moya... Interesting

  • @MissaMelodica
    @MissaMelodica 5 років тому

    This video is awesome!!!

  • @MultiSimransingh
    @MultiSimransingh 4 роки тому

    I use strict rules for using MVVM, this will break that. Or not?

  • @mohamedmohsen8746
    @mohamedmohsen8746 5 років тому

    Very useful

  • @muhammedalkabani3225
    @muhammedalkabani3225 4 роки тому

    Hi
    What if we need to use Alamofire with responseDecodabl?

  • @rasheshbosamiya1482
    @rasheshbosamiya1482 5 років тому

    Nice tutorial.

  • @RobertMason
    @RobertMason 5 років тому

    I have a question, I am hoping you or someone here can help me. I need to send a POST request to a server that is both json and base64 encoded. The problem being, when I attempt to assign the base64encoded string to the request.httpBody I get the error 'cannot assign value of type String to type Data?'. I have tried using request.httpBody = Data(jsonData!).base64EncodedData(), but the server is getting an empty array and the request fails. I can't find a way to actually send base64 encoded string in a POST request. Any help would be really appreciated. Thank you.

  • @david-tracy
    @david-tracy 5 років тому

    Good stuff

  • @sanjayravi2045
    @sanjayravi2045 5 років тому

    Can you make a tutorial on how to create search UI in table view with API data

    • @LetsBuildThatApp
      @LetsBuildThatApp  5 років тому +1

      Yes, you can find a lesson here www.letsbuildthatapp.com/course_video?id=4662

  • @donathmm3881
    @donathmm3881 5 років тому

    Hey how do you select these lines so fast with the keyboard at 06:05 ?

    • @Adam-cq1ch
      @Adam-cq1ch 5 років тому

      Shift + down arrow

    • @LetsBuildThatApp
      @LetsBuildThatApp  5 років тому

      I just hold SHIFT and hit DOWN.

    • @donathmm3881
      @donathmm3881 5 років тому

      @@LetsBuildThatApp Thanks. Can you make a new keyboard shortcut video to just code very fast instead of clicking etc with the trackpad.

    • @LetsBuildThatApp
      @LetsBuildThatApp  5 років тому +2

      I just a mouse and full size keyboard, its much easier than typing on a laptop and trackpad.

  • @maxcodes
    @maxcodes 5 років тому

    Thanks Brian

  • @rrakesh05
    @rrakesh05 5 років тому

    Nice Tutorial...

  • @rahulchaturvedi128
    @rahulchaturvedi128 5 років тому

    It is very helpful thanks

  • @XecuttioneR
    @XecuttioneR 5 років тому

    Does this work if success has more than one parameter?
    For example with the URLSession completion, it has data and response as “success”

    • @LetsBuildThatApp
      @LetsBuildThatApp  5 років тому

      You'll need to wrap the multiple values into your associated value class for the Result.

    • @XecuttioneR
      @XecuttioneR 5 років тому

      Lets Build That App oh I see, thank you!

  • @siddhantnigam4627
    @siddhantnigam4627 5 років тому

    nice and helpfull. Thank you

  • @seanrobinson7532
    @seanrobinson7532 5 років тому

    Have you tried using DispatchGroup to turn callbacks into an async-await style function? It is by far the best way to handle callbacks. So I enjoy the video explaining the new Result type in Swift 5, but I think you should mention that there are better ways to handle callbacks.

    • @LetsBuildThatApp
      @LetsBuildThatApp  5 років тому

      I like async-awaits as much as the next guy, it's disappointing that this doesn't come native with Foundation. That being said, the Result type has its place and can diversify your API toolset a lot.

    • @seanrobinson7532
      @seanrobinson7532 5 років тому

      @@LetsBuildThatApp Can you clarify a little bit? I think you could replace any function callback with an async-await style function and make the code more readable using DispatchGroup which is included in Foundation.

    • @LetsBuildThatApp
      @LetsBuildThatApp  5 років тому

      The purpose of a dispatch group is to synchronize a group of asynchronous calls and provide a proper callback mechanism when all tasks are finished. Even though they are similar, I wouldn't necessarily conflate DispatchGroup with the async-await pattern.
      Out of curiosity, how would you handle the potential errors of 3 async calls using a top down DispatchGroup block of code?

    • @seanrobinson7532
      @seanrobinson7532 5 років тому

      I've written some code in a Playground to share with you, but I am not sure the best way to send it to you. Any ideas?

    • @LetsBuildThatApp
      @LetsBuildThatApp  5 років тому

      GitHub link

  • @nursultanyelemessov480
    @nursultanyelemessov480 5 років тому

    Hi i am trying to do a NEWS APP and i faced this problem!
    Failed to download data: typeMismatch(Swift.Array, Swift.DecodingError.Context(codingPath: [], debugDescription: "Expected to decode Array but found a dictionary instead.", underlyingError: nil))
    I tried to find nil as i saw some different answers in stack overflow but nothing!

    • @mrs9278
      @mrs9278 5 років тому

      You need to double-check your data model - Looks like you've got it wrong
      Look at this for an example - stackoverflow.com/questions/52265674/expected-to-decode-int-but-found-a-string

  • @miladxyz
    @miladxyz 5 років тому

    How can I make it generic?

  • @BarisSaglam0
    @BarisSaglam0 5 років тому

    I start to learn developing IOS apps with Swift. I want to ask something. I always use libraries like 'Moya' or 'Alamofire' to request API. What do you think should i use libraries or is NSURLSession enough ?

    • @bhuwin6186
      @bhuwin6186 4 роки тому

      Barış Sağlam nsurlsession does its job very well so you can easily use it

  • @sreelakshmi2648
    @sreelakshmi2648 5 років тому

    Hi,
    i am using swift 5, i am beginner to iOS, i am using WKWebview to convert for my web application to iOS app,
    in my application i have social login options like facebook and google login, when i successful log In, Facebook login popup or google login page not closing, i need add close button or back button. is it possible, please help me, thanks in advance.

  • @Zainanjum100
    @Zainanjum100 5 років тому

    Thanks a lot

  • @davidlangley9287
    @davidlangley9287 5 років тому

    Nice!

  • @MoeSawalha-
    @MoeSawalha- 5 років тому +1

    We can already do this in swift 4.2 .. Result

  • @sivakrishna5293
    @sivakrishna5293 5 років тому

    Awesome

  • @SuperAbins
    @SuperAbins 5 років тому

    hi
    When i import Alamofire and try "Result" . I have got an error
    Generic type 'Result' specialized with too many type parameters (got 2, but expected 1).
    when i delete Alamofire Import there is no problem ?

    • @LetsBuildThatApp
      @LetsBuildThatApp  5 років тому +1

      This is because it's using Alamofire's Result class, instead of native UIKit version. It took me a while to figure this out too.

  • @bholeKBhakt
    @bholeKBhakt 5 років тому

    How generic work with result type

  • @smackaroony
    @smackaroony 5 років тому

    nice

  • @TheHacka2000
    @TheHacka2000 5 років тому

    Its definitely a lot cleaner but it almost seems like its performing the same task (as in checking if an error is present and reporting nothing) still interesting nonetheless!!

    • @LetsBuildThatApp
      @LetsBuildThatApp  5 років тому

      It’s a rather subtle difference but definitely a good one.

  • @yeilmusic
    @yeilmusic 5 років тому

    roulette app tutorial

  • @lucienchu9649
    @lucienchu9649 5 років тому

    This new features remind that how Vue.js do the REST, basically that's the same idea.

  • @abogdzie
    @abogdzie 5 років тому

    It’s not compatible with Objective-C unfortunately.

    • @LetsBuildThatApp
      @LetsBuildThatApp  5 років тому

      Indeed, objc is great but lacks modern programming language features.

  • @mrchedda
    @mrchedda 5 років тому

    Great video but I think reverb is unnecessary. I vote for no reverb. Thanks

    • @LetsBuildThatApp
      @LetsBuildThatApp  5 років тому +1

      New mic, getting used to how to adjust the sound but will tone down the post processing.

    • @mrchedda
      @mrchedda 5 років тому

      @@LetsBuildThatApp Brian.... I would suggest using only compression and eq during post. I am a believer that reverb on vocals should be reserved for songs only. But that's just me. Good luck!

  • @NEW-sf7cc
    @NEW-sf7cc 3 роки тому +1

    this doesn't work if you are already using Generic types for your decoding. like completion( Result) you need to mention the class you are decoding like Result, too bad

    • @LetsBuildThatApp
      @LetsBuildThatApp  3 роки тому +1

      It still works, you just need to specify the result type in your closure block.

    • @NEW-sf7cc
      @NEW-sf7cc 3 роки тому

      @@LetsBuildThatApp hey thanks for the reply, I actually imported Alamofire also in that same class and it was picking up Alamofire’s Result instead of Apple’s Result Enum, removing Alamofire import solved it for me

  • @TheAhmadReza
    @TheAhmadReza 5 років тому +1

    Thanks but I can't get this tutorial, its very long and breathtaking!

  • @vladimirprigozhanov5335
    @vladimirprigozhanov5335 5 років тому

    Who is this man which put dislike to this video? 🤣🤣🤣

    • @aleemun
      @aleemun 5 років тому

      Anyone who is from a nodeJs or Python background will dislike this(language not this video) for its verbosity.

  • @arnaudDR400
    @arnaudDR400 5 років тому

    Thanks for the video, Brian. the more it goes, the less I find swift easier to read or to use compared to Obj-C. I think I'm a dino!

  • @Dennisvm82
    @Dennisvm82 5 років тому

    Thank you for sharing this, Brian! Great explanation as well :-) What should I do with the completion call to make my fetchUsers function below work?
    fileprivate func fetchGenericJSONData(urlString: String, completion: @escaping (Result) -> ()) {
    guard let url = Bundle.main.url(forResource: urlString, withExtension: "json") else { return }
    if let data = try? Data(contentsOf: url) {
    do {
    let objects = try JSONDecoder().decode(T.self, from: data)
    completion(.success(objects))
    } catch let jsonError {
    completion(.failure(jsonError))
    }
    }
    }
    func fetchUsers(completion: @escaping ([User]?, Error?) -> ()) {
    fetchGenericJSONData(urlString: "Users", completion: completion)
    }

    • @Dennisvm82
      @Dennisvm82 5 років тому

      Forget I asked :-P had to add the same Result type to the other function and remove the optional Error.

    • @pradeepkumar-tm5et
      @pradeepkumar-tm5et 5 років тому

      @@Dennisvm82 Hi I am also facing the issue while using Result type in generic types can you share the code how is it working..

    • @Dennisvm82
      @Dennisvm82 5 років тому

      @@pradeepkumar-tm5et Hi Pradeep! Check out my code below :-) I am working with local JSON files for testing purposes, so my generic function is a little bit different. The parameters should be the same I believe. I hope my code can help you out!
      func fetchUsers(completion: @escaping (Result) -> ()) {
      fetchGenericJSONData(urlString: "Users", completion: completion)
      }
      fileprivate func fetchGenericJSONData(urlString: String, completion: @escaping (Result) -> ()) {
      guard let url = Bundle.main.url(forResource: urlString, withExtension: "json") else { return }
      if let data = try? Data(contentsOf: url) {
      do {
      let objects = try JSONDecoder().decode(T.self, from: data)
      completion(.success(objects))
      } catch let jsonError {
      completion(.failure(jsonError))
      }
      }
      }
      fileprivate func fetchUserData() {
      Service.shared.fetchUsers { (result) in
      switch result {
      case .success(let user):
      self.userViewModel = user ?? []
      case .failure(let err):
      print("Failed to fetch data:", err)
      }
      }
      }

    • @pradeepkumar-tm5et
      @pradeepkumar-tm5et 5 років тому

      @@Dennisvm82 Than you very much. You have resolved my issue...

    • @Dennisvm82
      @Dennisvm82 5 років тому

      @@pradeepkumar-tm5et You are welcome :-)

  • @ayon3527
    @ayon3527 5 років тому

    You don’t type faster. you just type which happens to be fast.😁

    • @LetsBuildThatApp
      @LetsBuildThatApp  5 років тому +1

      Yeah, I just can't stand slow typers when I watch tutorials.

  • @marcohenriquemaiamarques9031
    @marcohenriquemaiamarques9031 5 років тому

    Thank you Brian for this tutorial.

  • @lucasroselli3379
    @lucasroselli3379 5 років тому

    Great video, as always