No video

Swift 5 Brand New Result Type: Write Cleaner API code

  Рет қаралды 55,034

Lets Build That App

Lets Build That App

5 жыл бұрын

Great to see small little details still being added to the Swift 5 programming language such as the brand new Swift 5 Result Type. It's super useful but takes a little while to get used to how to set up the syntax correctly. This lesson will shed some light on how the code is written. Enjoy.
AppStore JSON APIs
www.letsbuildthatapp.com/cour...
Tinder Firestore Swipe and Match
www.letsbuildthatapp.com/cour...
Twitter Slide Out Menu Course
www.letsbuildthatapp.com/cour...
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

Пікірлер: 117
@LohenYumnam
@LohenYumnam 5 жыл бұрын
This video is by far the most mind clearing tutorial on Swift Result type. good job Brian.
@christostsangaris4785
@christostsangaris4785 5 жыл бұрын
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!
@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!
@rajanmaheshwari
@rajanmaheshwari 5 жыл бұрын
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
@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
@saschasimon812
@saschasimon812 5 жыл бұрын
I did not know abouzt this feature, but it will make my callbacks a lot easier and more readable - thank you!
@Ipadstands
@Ipadstands 5 жыл бұрын
It is just fun to watch your videos . It will be a few months before I can/would use this.
@WinstonSalcedo
@WinstonSalcedo 5 жыл бұрын
Thank you Brian for this tutorial.
@robertotomas
@robertotomas 5 жыл бұрын
You, sir, get mad kudos for this
@krutomjer
@krutomjer 5 жыл бұрын
Very nice, I’ll implement this tomorrow since I’m refactoring some network code. Thanks Brian
@kama8213
@kama8213 4 жыл бұрын
Number one favorite tutor!
@donathmm3881
@donathmm3881 5 жыл бұрын
Thank you for that great tutorial
@olmosbekkhurramov5259
@olmosbekkhurramov5259 2 жыл бұрын
Thank you Brain! This video help me
@lucasroselli3379
@lucasroselli3379 5 жыл бұрын
Great video, as always
@alondob
@alondob 5 жыл бұрын
Glad to see this make it into Swift.
@tieda
@tieda 5 жыл бұрын
Waiting for this! :)
@MissaMelodica
@MissaMelodica 5 жыл бұрын
This video is awesome!!!
@MrJAEChannel
@MrJAEChannel 5 жыл бұрын
Great video man
@sourabhgajbhiye3574
@sourabhgajbhiye3574 5 жыл бұрын
Awesome tutorial 😎
@badakml7366
@badakml7366 4 жыл бұрын
thanks man, it makes my alamofire implementation more cleaner
@siddhantnigam4627
@siddhantnigam4627 5 жыл бұрын
nice and helpfull. Thank you
@rahulchaturvedi128
@rahulchaturvedi128 5 жыл бұрын
It is very helpful thanks
@mohamedmohsen8746
@mohamedmohsen8746 5 жыл бұрын
Very useful
@rasheshbosamiya1482
@rasheshbosamiya1482 5 жыл бұрын
Nice tutorial.
@Cdswjp
@Cdswjp 4 жыл бұрын
Good stuff
@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.
@maxcodes
@maxcodes 5 жыл бұрын
Thanks Brian
@rrakesh05
@rrakesh05 5 жыл бұрын
Nice Tutorial...
@Zainanjum100
@Zainanjum100 5 жыл бұрын
Thanks a lot
@davidlangley9287
@davidlangley9287 5 жыл бұрын
Nice!
@hiteshrasal5222
@hiteshrasal5222 5 жыл бұрын
Hi nice tutorial..can you please made tutorial on clean architecture with API request example?
@sivakrishna5293
@sivakrishna5293 5 жыл бұрын
Awesome
@VertoSH
@VertoSH 5 жыл бұрын
Oh so they made "native" the result framework we had to import to use alamofire and Moya... Interesting
@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
@user-de5qn6cz3f
@user-de5qn6cz3f 5 жыл бұрын
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 жыл бұрын
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.
@charlesm.1638
@charlesm.1638 4 жыл бұрын
Can I call this from a class located in another Swift File? Trying to focus on OOP in Swift.
@muhammedalkabani3225
@muhammedalkabani3225 4 жыл бұрын
Hi What if we need to use Alamofire with responseDecodabl?
@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
@smackaroony
@smackaroony 5 жыл бұрын
nice
@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?
@dany3370
@dany3370 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
@FuckinHostileMerol
@FuckinHostileMerol 5 жыл бұрын
Nice! But I prefer Promisekit
@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 ?
@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.
@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 :)
@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.
@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.
@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
@ridhomuhammad17
@ridhomuhammad17 5 жыл бұрын
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
@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 жыл бұрын
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.
@MoeSawalha-
@MoeSawalha- 5 жыл бұрын
We can already do this in swift 4.2 .. Result
@MultiSimransingh
@MultiSimransingh 4 жыл бұрын
I use strict rules for using MVVM, this will break that. Or not?
@LetsBuildThatApp
@LetsBuildThatApp 4 жыл бұрын
Try it out and see
@sanjayravi2045
@sanjayravi2045 5 жыл бұрын
Can you make a tutorial on how to create search UI in table view with API data
@LetsBuildThatApp
@LetsBuildThatApp 5 жыл бұрын
Yes, you can find a lesson here www.letsbuildthatapp.com/course_video?id=4662
@miladxyz
@miladxyz 5 жыл бұрын
How can I make it generic?
@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!
@bholeKBhakt
@bholeKBhakt 5 жыл бұрын
How generic work with result type
@DanielPaysTheMortgage
@DanielPaysTheMortgage 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
@DanielPaysTheMortgage
@DanielPaysTheMortgage 5 жыл бұрын
@@LetsBuildThatApp Will do, thanks for the reply, you are an amazing teacher!
@nursultanyelemessov480
@nursultanyelemessov480 4 жыл бұрын
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 4 жыл бұрын
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
@sreelakshmi2648
@sreelakshmi2648 4 жыл бұрын
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.
@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 жыл бұрын
I just a mouse and full size keyboard, its much easier than typing on a laptop and trackpad.
@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
@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!
@lucienchu9649
@lucienchu9649 5 жыл бұрын
This new features remind that how Vue.js do the REST, basically that's the same idea.
@TheHacka2000
@TheHacka2000 4 жыл бұрын
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 4 жыл бұрын
It’s a rather subtle difference but definitely a good one.
@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.
@yeilmusic
@yeilmusic 5 жыл бұрын
roulette app tutorial
@TheAhmadReza
@TheAhmadReza 5 жыл бұрын
Thanks but I can't get this tutorial, its very long and breathtaking!
@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 жыл бұрын
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!
@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 жыл бұрын
Yeah, I just can't stand slow typers when I watch tutorials.
@NEW-sf7cc
@NEW-sf7cc 3 жыл бұрын
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 жыл бұрын
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
@marcohenriquemaiamarques9031
@marcohenriquemaiamarques9031 5 жыл бұрын
Thank you Brian for this tutorial.
Advanced Swift Generics: Best Solution to Eliminate Code Duplication!
17:18
Lets Build That App
Рет қаралды 38 М.
When to Semaphore vs Dispatch Group! Careful Multithreaded Shared Resource
11:11
Викторина от МАМЫ 🆘 | WICSUR #shorts
00:58
Бискас
Рет қаралды 5 МЛН
Son ❤️ #shorts by Leisi Show
00:41
Leisi Show
Рет қаралды 8 МЛН
Use Closures Not Delegates | Swift 5, Xcode 10
14:52
Kilo Loco
Рет қаралды 24 М.
What's going on with Windows Laptops?
10:30
Marques Brownlee
Рет қаралды 2,7 МЛН
Enum Associated Values + Swift Result Type
21:09
Stewart Lynch
Рет қаралды 4 М.
Loops and Hash Maps Job Preparation Interview Question
15:55
Lets Build That App
Рет қаралды 20 М.
Completion Handlers
17:06
Stewart Lynch
Рет қаралды 11 М.
Swift: completion escaping - замыкания
14:13
AvenCode iOS developer
Рет қаралды 3 М.
Advanced Swift: Making Programmatic Auto Layout Easy through Extensions
25:38
The Story of Next.js
12:13
uidotdev
Рет қаралды 560 М.
5 JavaScript Concepts You HAVE TO KNOW
9:38
James Q Quick
Рет қаралды 1,4 МЛН
Weak and Unowned Self Closure Memory Leak Fixes
12:21
Lets Build That App
Рет қаралды 56 М.