Swift - Result Type - Cleaner Network Calls

preview_player
Показать описание

In today's video I show you how to use Swift's Result type that was introduced in Swift 5. In this video we clean up an old, ambiguous network call that relied on optionals. Using Swift's Result type eliminates the optionals and makes things very clear at the call site.

Swift Result Documentation:

If you like my teaching style, I've released my first iOS dev course which is all about the take-home project job interview for iOS developers.

Link to my book - How I Became an iOS Developer:

Check out my podcast, iOS Dev Discussions:

Are you a gamer? I'm streaming video games on Twitch:

Book and learning recommendations that help out the channel if you decide to purchase (Affiliate Links):

Ray Wenderlich Books:

Ray Wenderlich Videos:

Paul Hudson's Hacking With Swift:

Learn Advanced Swift Here:

Links to my iOS Dev Setup & iOS Dev Book Recommendations

Personal Links:

Twitter:

Instagram:
@seanallen_dev

#swift #softwaredeveloper #iosdeveloper
Рекомендации по теме
Комментарии
Автор

Awesome, Sean. Please do more of these non-beginner videos. Big fan of these

guitaripod
Автор

I definitely need to use result type more often 😄

hectorsvill
Автор

Pretty good tutorial, this will make my networking calls a lot more cleaner!

masterofyngwie
Автор

I had some troubles with passing server direct errors to user with this method but finally it's working, Thank you Sean.

TheAhmadReza
Автор

Great 👍, I love this new feature too.
In error type, better to use errors conforming to LocalizedError protocol

Ipat
Автор

Cleaner networking!
Thanks, Sean great video!

MikaelaCaron
Автор

Loving this content! A lot of stuff to learn during these times to stay productive. Thanks, Sean!

lianahaque
Автор

great tutorial and excellent explanation, I really enjoyed watching it, keep it up

abdulazizalharbi
Автор

Thanks Sean, my network call was incomplete and I didn’t notice that, I definitely you change my code looks more cleaner and easy to read.

finalpets
Автор

The timing of this video is superb lol

guitaripod
Автор

That's a great help. Thank You ⭐️

roushilsingla
Автор

Great video! I usually have two completion handlers: A successful one and a failure one (is that bad?) and I call the one that makes sense. I think I'll change to this solution :) I really liked that! Thanks for the tip. I am a new follower and your channel is helping me a lot!

juliayamamoto
Автор

Great video as always. Also, may I know how do you keep your code formatting so clean? My code is messy and it gets even worse when there is a code snippet copied over from other places. Do you use any tool to format the code or just do it manually? Thanks!

soullesessess
Автор

Good video. But I think depending on the use case, the old way is still valid. Imagine, if you have several method calls
one after the other, which can fail. So, essentially you "jump" out of the success case right after the switch statement and call the next failable method OR you nest calls in the success case. So, that is were guard let comes into play. You first handle all the failure cases of the method calls and after that you can cleanly code your success case.
So, it's good that we have another way of dealing with failures, Maybe in future there will be some common way on how to use the new result type in the case of several failable methods

manfredkernMK
Автор

Great video sean.! For Network Call you were using class is there any reason for that or we can use struct?

salithaherath
Автор

I've found this works best when combining Result, DataTask, and Codable with generics. Essentially decoding your network response data as the lhs of your completion closure. I've got plenty of code to share if you want to do a follow up.

alexchase
Автор

really thank you I need it, made the life cleaner and esaier

nawafb
Автор

Very useful your videos as always. I have only one recommendation. I think isn’t good create a whole method for each API call, you should create a generic type instead so you wouldn’t have many method with the same code.

VictorRoldanvictorroldan
Автор

Very cool stuff. Out of curiosity, what's in the presentGFAlertOnMainThread method?

cristianm.
Автор

Hi @sean, I’m a big fan of ur videos. I implemented result type in my project and I’ve been having a hard time wrapping my head around it.

What i do before is i create a model for my 200 and 400 response so as to display a toast containing my response but I’ve been unable to decode and pass the 400 error to my enum.

I really need your help.

tolulopeaboyeji