quick stupid question, i know this video is about Error Handling - Do, Try, Catch but what if i in this particular situation do it with a simple if else in loginButtonTapped to just show the appropriate alert? will it be a bad code?
I love this series for it's organization and simplicity. There are far too many hour long tutorials on these fundamental concepts. You explain it well and show people it's not as hard as many people make it out to be. Subscribed. Cheers
Thanks for putting this example into context. Please continue to do your videos so that it's easier to understand where to use these programming concepts.
Hey Mr. Allen, firstly, thank you for everything. I don't know how I discovered you has a wonderful, simple and understandable explanation. I'm a CS student in Ankara Bilkent University. I really wanted to learn swift but it was very complex until I find you. I cannot explain how happy I am, please continue to create these great videos ,series and keep it complete and simple. Thank you again. (sorry for the grammar mistakes)
Well I'm a beginner developer and I really needed to see some videos about iOS development and etc. I found your channel and enjoying so far. Thanks man :)
Nice explanation Sean! I’m really enjoying your videos! I know it’s crystal clear to everybody, but please note that the magically code that actually logs you in will be performed asynchronously in the background (obviously). So make sure to handle the successful login request properly, and put your transition code there, instead of simply putting them the next line right after try login(). Otherwise the control will be given back to your do block, where the remaining code will be executed before the login response arrives.
Thanks for the kind words, Mate! You are correct. I didn't go into all this because I didn't want to distract from the point of the video too much, but it is a good clarification.
bro dont have words you are just great at explaining some people are flaunting their talent by making complex code i agree they know some good stuff but you make swift look easy #highlymotivated
Hey Yaroslav, thanks for the feedback. I didn't forget about Try! and Try? I just decided no to discuss them for a couple of reasons. 1. The video was already getting a little long 2. The topic of the video is Error Handling in a Do, Try, Catch block. When using Try! and Try?, you are essentially choosing to not handle the errors so it wasn't 100% on topic. The combination of those 2 reasons are why I decided to cut that topic.
Thanks Sean, This video is very helpful , Can you please make sure that if their is any way to write all error cases in another swift file ? If it is possible, please give an example as I am new in swift so I am facing some problem to write this code.
Hi thank you for this video, I have never seen both enum error cases, and a do try catch being used in the same function, were you just giving an example of how to use both, or do you usually do that only when there is a throw included? I ask because I imagine that if there is a throw included inside of the function, we only really need to use a do try catch block and can print out the error to do displayed to the user and do not need the enum cases? does this make sense ? Which is preferred?
Thanks for all your work on your channel! Quick question: could we instead of catching each error separately have a single ´´´catch let error { ... } ´´´ and do a switch on the error property inside?
I've actually never implemented it that way, but that sounds viable. Huge disclaimer... I haven't researched that and could be wrong. I would need to dig into it a bit more, but off the top of my head, it sounds alright.
Hi Sean .your videos are awesome .teaching skills are great.Please upload some videos for Core Data,Fetch Controller,Fetching large no of records from API ,Writing into Core Data and Displaying them into Table View concurrently.Thanks In Advance.
Hey Shashi, Thanks for the kind words! Glad you're enjoying the channel. As for Core Data tutorials, I probably won't be doing that anytime soon. I've never used it, and I just haven't seen the adoption in my experience to make it worth while. From my experience here in Silicon Valley, it isn't used all that often on production level projects. (Don't get me wrong, I'm not saying it's NEVER used... just pretty rare).
Hey Sean, very nice tutorial, just download code . I wonder how did you setup the constraint of the all ui elements, did you setup constraint for each ui element, or you use suggested constraint for all. Maybe a tutorial about view constraint next time?
Can I ask about the regular expression for the email validation? I see that [A-Z0-9a-z._%+-] means any of those characters in the brackets and the + means one or more times, then you have the @ symbol which makes sense. After the @ symbol, you have [A-Za-z0-9.-]+ which is basically the same thing as above without a few of the special characters. My question starts here: \\. I know that \. means "." because it has to escape, but why the extra "\"? I think I also understand the end: [A-Za-z]{2,64} means A-Z and/or a-z anywhere from 2 to 64 times, right? Thanks! This is really helpful; it's the first time I've seen regular expressions in Swift and honestly didn't know it was possible before this video.
Hey Brandon, I'm not gonna lie and act like I fully understand all the ins and outs of regular expressions. It's a tricky topic to wrap your head around. I got this code snippet from a senior iOS dev a couple years ago and have been using it every since. Sorry I couldn't be of more help, but I would have to do more research before answering correctly.
I've really only used MVC, and payed around with MVVM so I'm a bit biased towards MVC. My thought's on this subject are that it's a personal preference and would depend on your team and the project. Certain architectures may be better for certain projects.
Hi Sean. Thank you for the great tutorial. Is there a way to throw nothing in a function that throws? e.g (I don't want to create class or enum inherit from Error and return that object.)
Hey Sean. As a beginner, I'd like to ask you something. When you started learning, did you focus on the language alone (swift) and OOP concepts, and then moved to more core issues like algorithms? Is there any tip you could give me as a beginner? What would be the best way to learn swift and get better understanding of OOP?
Hey Sophie, I started learning the language and OOP concepts first. I didn't get around to data structures and algorithms until it was time to apply for an iOS Developer position. And that was mainly because you need to know that stuff more so for the interview than the actual job. So as a beginner, I would definitely focus on learning Swift, the language, as well as basic OOP principals. I would start by doing tutorials on raywenderlich.com and then start building small apps on your own. I'm a big believer on "learning by doing", and you'll learn a lot when you try to build your own app idea.
I am learning too, one thing I think is important is to be able to speak in the Swift language and not use terms like "this", "that" and "stuff". Everything has it's own name like "variable", "operand", "nil coalescence" etc..
Hi Sean, I'm a beginner in Swift, and I'm wondering how did you able to use the function in class Alert without instantiating it? or i just overlooked it somewhere? sorry for newbie question..
I think this comes down to personal preference. They both have their pros and cons in my opinion. Storyboards can be a pain when working with a team due to merge conflicts and code review. However, I prefer using Storyboards because I don't like "clutter' in my code. And all the UI and auto layout code can clutter up a file in my opinion. But again, it's personal preference. If you're just learning, you'll learn a lot more by doing your UI in code. However, once you understand what's going on, doing your UI in storyboard is cleaner in my opinion.
Hello Sean, if I understood try catch is only for throwable methods. But how I can catch error from nonthrowable function or method. Example: let a = String(data: someData, encoding: .utf8) . Some times it it crash and application crash. Can I catch this error? Thank you.
In the example you provide, functions like that from Apple often return an optional. The reason it's crashing for you is because you're not unwrapping the optional correctly. So when the result of that function is nil, you'll get a crash.
Glad you like my teaching style. These are some of my older videos and I've actually gotten a lot better. If you're not happy with Udemy courses, I started creating courses at seanallen.teachable.com if you want to check them out. You can watch the first ~10% of each one as a free preview.
Basically a method that takes a small tasks and puts it into it's own method. Even tho this is just a 30 second preview, the video description and transcript should give you an idea: teamtreehouse.com/library/helper-methods
It would depend on the situation. The main reason you would use GUARD is if you wanted to exit the function as a result of the "failed" guard. Guards provide an "early exit" from the function if you want it.
Hi Sean! Have a question: In case - we don't need error (wich in catch block) - can we use something like: let isLoggedIn = try? login() ?? false (login() returns true in case of success)?
I'm not sure of the exact code you're looking for, but in general, if you're not going to handle the error then you don't need to full do, try, catch block;
When an iOS job applicant is at an onsite interview are they generally allowed to bring a paper notebook or iPad to use as a reference during the coding exercises? (algorithms excluded)
@@seanallen Anyway, do you think that trying to get into ios carreer is a good idea in present times? I want to try send come resume in next month in my country(Poland). I don't know how market look like. Unfortunately i don't see so many offers;/ at least in my area
hello, you variable isValidEmail : bool , then put the { } , what is this, you add a closure behind it, so it will be executed everytime ? sorry i am a newbie, see this sometimes, don't know wut it is officially called ?
This is called a "computed property". Which means the property isn't a set value, like for example "var myNumber = 1". In that case, "myNumber" is always 1 (unless you change it). In a computed property, the value needs to be figured out. It's figured out by whatever logic you write in the {}
imo it would be better to handle the title and message of the error inside the error enum, that way you can just call the alert message once. Nevertheless, probably the best video I've come across on error handling.
Can you give me an example of how would you pass both title and message with enum value? I can add an associated rawValue, but only of a simple type (Int, String, Bool), but not a dictionary like: [String:String] Here is the only thing I could come up with, which is a bit ugly, imho: enum LoginError: String, Error { case incompleteForm = "Incomple Form:Please fill out both email and password fields" case invalidEmail = "Invalid Email Format:Please make sure you format your email correctly" case incorrectPasswordLength = "Password Too Short:Password should be at least 8 characters" } do { try login() // Transition to next screen } catch let error as LoginError where [.incompleteForm, .invalidEmail, .incorrectPasswordLength].contains(error) { let errorMessage = error.rawValue.split(separator: ":") Alert.showBasic(title: String(errorMessage[0]), message: String(errorMessage[1]), vc: self) } catch { Alert.showBasic(title: "Unable To Login", message: "There was an error when attempting to login", vc: self) }
Great content, but feel like they should be supplemented with examples of how they are applied within the context of iOS development similar to this video
Hey Sean, you mentioned that we use do,try,catch in cases where the function outputs some error. What is the advantage of using do,try, catch over an "if else" in this case. Is it that it makes the code looks better of is there some benefit in actual speed of execution or something like that ?
Sean, what your methodologies when coding? I feel that some times I get lost in code because I don't have a general sense of direction when a problem may arise. I'm trying to get better, typically would write python scripts but now that I am getting into this type of development and using more and more files such as classes to fulfill the application its hard planning. It can kinda be discouraging but I feel like its something common and all you need is the right sense of direction when stuck.
I know what you're saying. It was overwhelming at first. To be honest, there's not really an easy answer. It's something I've developed over a couple years, and I'm still building. (Talking about my methodologies). They are constantly evolving and improving. Coming up with how you like to structure your apps is something that comes with time an experience.
bro i have a request that i feel that once in a week for an hour you should interact with us for our queries or doubt if you can give one hours of your precious schedule it will be pleasure for us waiting for yours zoom , google meet link
Thanks for your video. I wih to ask one thing: How can I implemente an error handling in the code below? Presently it doesn't work because it doesn't throws errors. It gives these warnings, in xcode: No calls to throwing functions occur within 'try' expression and 'catch' block is unreachable because no errors are thrown in 'do' block Thank you for any help, if possible. DispatchQueue.main.async { do { self.name.text = MyAccountViewController.nameofuser self.email.text = MyAccountViewController.email self.sessions.text = "\(MyAccountViewController.active_device_sessions)/\(MyAccountViewController.max_devices_sessions)" self.plan.text = MyAccountViewController.userplan try MyAccountViewController.used_disk_space = ByteCountFormatter.string( fromByteCount: Int64(MyAccountViewController.used_disk_space)!, countStyle: ByteCountFormatter.CountStyle.file ) try MyAccountViewController.max_disk_space = ByteCountFormatter.string( fromByteCount: Int64(MyAccountViewController.max_disk_space)!, countStyle: ByteCountFormatter.CountStyle.file ) try self.space.text = "\(MyAccountViewController.used_disk_space)/\(MyAccountViewController.max_disk_space)" } catch let e { debugPrint("failed to get user data: \(e)") } }
Watch Next - iOS Take Home Project - Job Interview Practice - Free Preview - ruclips.net/video/MSIe2y6Fee8/видео.html
quick stupid question, i know this video is about Error Handling - Do, Try, Catch but what if i in this particular situation do it with a simple if else in loginButtonTapped to just show the appropriate alert? will it be a bad code?
Thank you, even after 6 years, your code and teaching is still actual
If I have to learn any difficult concept, I look up your videos. You explain it better than anybody else.
Thanks for the kind words. Happy to hear they are helpful!
I love this series for it's organization and simplicity. There are far too many hour long tutorials on these fundamental concepts. You explain it well and show people it's not as hard as many people make it out to be. Subscribed. Cheers
Thanks for the kind words, Tactteem00. Glad to hear the videos are helping. More on the way!
Awesome video! I like how you reinforced the "throw" keyword in there. 😃 The repetition makes it stick! Thanks, Sean.
Thanks Mark! I'm a strong believer in the power of repetition when learning... especially in programming!
Dude every question I have about iOs, you have a video on it... thx A LOT!!! Happy new years!
Happy to help, Javier!
Thanks for putting this example into context. Please continue to do your videos so that it's easier to understand where to use these programming concepts.
Glad you enjoyed it, Henry! More videos on the way after the Thanksgiving Holiday
This was by far one of the best and most practical explanations of Do/Try/Catch I have seen!
Thank Chris! Glad you enjoyed it.
Hey Mr. Allen, firstly, thank you for everything. I don't know how I discovered you has a wonderful, simple and understandable explanation. I'm a CS student in Ankara Bilkent University. I really wanted to learn swift but it was very complex until I find you. I cannot explain how happy I am, please continue to create these great videos ,series and keep it complete and simple. Thank you again. (sorry for the grammar mistakes)
Thanks for the kind words, Fikret... More videos coming soon!
my favorite series ... thanks a lot ...
Thanks for the kind words, Ahmed! I'll keep them coming!
Thanks for continuing your interview series.
You're welcome, Isaac.
Another great video from this series! Detailed and yet easy to understand. Thanks.
Glad you enjoyed it, John!
Well I'm a beginner developer and I really needed to see some videos about iOS development and etc. I found your channel and enjoying so far. Thanks man :)
Glad you're enjoying it Ege! Feel free to reach out with any questions if you get stuck on something.
Nice explanation Sean! I’m really enjoying your videos! I know it’s crystal clear to everybody, but please note that the magically code that actually logs you in will be performed asynchronously in the background (obviously). So make sure to handle the successful login request properly, and put your transition code there, instead of simply putting them the next line right after try login(). Otherwise the control will be given back to your do block, where the remaining code will be executed before the login response arrives.
Thanks for the kind words, Mate! You are correct. I didn't go into all this because I didn't want to distract from the point of the video too much, but it is a good clarification.
Best tutorial ever for swift error handling , really thank you.
Glad you liked it!
Crystal clear of do try catch! Thank you!
Glad it helped, Martin 👍
Your videos are just what i needed all the time
thank you
Happy to help, Sasan 👍
Short and simple... Superb :)
+Karunakar Bandikatla Glad you enjoyed it.
Love your videos man! You are putting out some great content. Thanks for your time and effort.
Thanks for the kind words, Art! It means a lot.
Thank you for the rapid explanation, Do, Try and Catch were confusing me.
I am surprised to see no less than three Bang Operators !
Very useful. Thanks Sean!
Nice Video!! Can you explain what is the difference between throws and rethrows?
Great Video Sean!
Thanks Brandon!
Another excellent share, thank for your the clear explanation
Thanks for watching all these Taylor. Glad they were helpful.
Good job! I finally got it
I love the Alert handling, well done. But I discover the "class func", what is it ?
bro dont have words you are just great at explaining some people are flaunting their talent by making complex code i agree they know some good stuff but you make swift look easy #highlymotivated
Nice! But forgot to mention the try! And try? There some cases to use it as well
Hey Yaroslav, thanks for the feedback. I didn't forget about Try! and Try? I just decided no to discuss them for a couple of reasons. 1. The video was already getting a little long
2. The topic of the video is Error Handling in a Do, Try, Catch block. When using Try! and Try?, you are essentially choosing to not handle the errors so it wasn't 100% on topic.
The combination of those 2 reasons are why I decided to cut that topic.
Great tutorial, thanks a lot :)
Glad you liked it, Saad!
Thanks Sean,
This video is very helpful ,
Can you please make sure that if their is any way to write all error cases in another swift file ?
If it is possible, please give an example as I am new in swift so I am facing some problem to write this code.
Very helpful!!!!! Thank you very much! :)
Happy to help, Fabio. You're very welcome.
Hi thank you for this video, I have never seen both enum error cases, and a do try catch being used in the same function, were you just giving an example of how to use both, or do you usually do that only when there is a throw included? I ask because I imagine that if there is a throw included inside of the function, we only really need to use a do try catch block and can print out the error to do displayed to the user and do not need the enum cases? does this make sense ? Which is preferred?
Very helpful video
Happy to help, Mohsin 👍
Excellent example for "throw"
Bro u are too good. How long have u been coding swift ?
Since early 2015
@@seanallen wonderful. How many hours a day? Anyways I followed u on Twitter.
Thanks for all your work on your channel!
Quick question: could we instead of catching each error separately have a single ´´´catch let error { ... } ´´´ and do a switch on the error property inside?
I've actually never implemented it that way, but that sounds viable. Huge disclaimer... I haven't researched that and could be wrong. I would need to dig into it a bit more, but off the top of my head, it sounds alright.
Thank you!
Happy to help, Donat!
thanks for making these videos!! could you do one on regular expressions in swift and how to use them to validate data users put in text fields?
Hi Sean .your videos are awesome .teaching skills are great.Please upload some videos for Core Data,Fetch Controller,Fetching large no of records from API ,Writing into Core Data and Displaying them into Table View concurrently.Thanks In Advance.
Hey Shashi, Thanks for the kind words! Glad you're enjoying the channel. As for Core Data tutorials, I probably won't be doing that anytime soon. I've never used it, and I just haven't seen the adoption in my experience to make it worth while. From my experience here in Silicon Valley, it isn't used all that often on production level projects. (Don't get me wrong, I'm not saying it's NEVER used... just pretty rare).
Great video. Thanks
Great video, love the speed of your speech. I hate slow talkers!
Glad you liked it! I get a lot of comments that I talk too fast.
hey can u explain how to use nspredicate to filter strings
Is there a way to define error messages in enum and just use one catch and throw relevant error message ?
Nice video again,sean how we can implement pager in ios like android
I've never used an Android phone, so I'm not sure what behavior you're talking about.
Its Like tab bar in ios ,Match with watsapp app what they used instead of tab bar in android
It is kind of like a scroll view. link-developer.android.com/training/animation/screen-slide.html.
Ok, now I get it. Looks like a scroll view or UIPageViewController in iOS.
love u brother from another mother !!
Glad you liked it!
Hey Sean, very nice tutorial, just download code . I wonder how did you setup the constraint of the all ui elements, did you setup constraint for each ui element, or you use suggested constraint for all. Maybe a tutorial about view constraint next time?
I set up the constraints for each UI element individually in the storyboard. A how to video for constraints is high on my list of videos to make.
Can I ask about the regular expression for the email validation?
I see that [A-Z0-9a-z._%+-] means any of those characters in the brackets and the + means one or more times, then you have the @ symbol which makes sense. After the @ symbol, you have [A-Za-z0-9.-]+ which is basically the same thing as above without a few of the special characters.
My question starts here: \\. I know that \. means "." because it has to escape, but why the extra "\"?
I think I also understand the end: [A-Za-z]{2,64} means A-Z and/or a-z anywhere from 2 to 64 times, right?
Thanks! This is really helpful; it's the first time I've seen regular expressions in Swift and honestly didn't know it was possible before this video.
Hey Brandon, I'm not gonna lie and act like I fully understand all the ins and outs of regular expressions. It's a tricky topic to wrap your head around. I got this code snippet from a senior iOS dev a couple years ago and have been using it every since. Sorry I couldn't be of more help, but I would have to do more research before answering correctly.
Hello guys ! Did you find the answer to this question? Thank you!
Hi Sean, What's best architecture for developing longterm product?
Now i'm using Viper for my product.
I've really only used MVC, and payed around with MVVM so I'm a bit biased towards MVC. My thought's on this subject are that it's a personal preference and would depend on your team and the project. Certain architectures may be better for certain projects.
awesome stuff. thanks a lot for this content :)
Hi Sean. Thank you for the great tutorial. Is there a way to throw nothing in a function that throws? e.g (I don't want to create class or enum inherit from Error and return that object.)
is it possible to put the enum in a separate file? I just started learning swift.
Hey Sean. As a beginner, I'd like to ask you something. When you started learning, did you focus on the language alone (swift) and OOP concepts, and then moved to more core issues like algorithms? Is there any tip you could give me as a beginner? What would be the best way to learn swift and get better understanding of OOP?
Hey Sophie, I started learning the language and OOP concepts first. I didn't get around to data structures and algorithms until it was time to apply for an iOS Developer position. And that was mainly because you need to know that stuff more so for the interview than the actual job. So as a beginner, I would definitely focus on learning Swift, the language, as well as basic OOP principals. I would start by doing tutorials on raywenderlich.com and then start building small apps on your own. I'm a big believer on "learning by doing", and you'll learn a lot when you try to build your own app idea.
I am learning too, one thing I think is important is to be able to speak in the Swift language and not use terms like "this", "that" and "stuff". Everything has it's own name like "variable", "operand", "nil coalescence" etc..
Great.. I’d expect if you include odd scenarios too, at the end of each video
Is the isValidEmail extension written in a mix of Swift and Objective C? The emailFormat looks akin to it
Nope, it's all in Swift. The weird syntax you see is the Regular Expression stuff. But it's all in Swift.
hey please make more videos on interview question and answers.
Hi Sean,
I'm a beginner in Swift, and I'm wondering how did you able to use the function in class Alert without instantiating it? or i just overlooked it somewhere? sorry for newbie question..
Hey! Do you suggest using storyboards or doing everything programmatically in big projects?
I think this comes down to personal preference. They both have their pros and cons in my opinion. Storyboards can be a pain when working with a team due to merge conflicts and code review. However, I prefer using Storyboards because I don't like "clutter' in my code. And all the UI and auto layout code can clutter up a file in my opinion. But again, it's personal preference. If you're just learning, you'll learn a lot more by doing your UI in code. However, once you understand what's going on, doing your UI in storyboard is cleaner in my opinion.
Hello Sean, if I understood try catch is only for throwable methods. But how I can catch error from nonthrowable function or method. Example: let a = String(data: someData, encoding: .utf8) . Some times it it crash and application crash. Can I catch this error? Thank you.
In the example you provide, functions like that from Apple often return an optional. The reason it's crashing for you is because you're not unwrapping the optional correctly. So when the result of that function is nil, you'll get a crash.
Really helpful vídeo man I Man doing a Udemy course but they don’t explains this that well
Glad you like my teaching style. These are some of my older videos and I've actually gotten a lot better. If you're not happy with Udemy courses, I started creating courses at seanallen.teachable.com if you want to check them out. You can watch the first ~10% of each one as a free preview.
Awsome) Thanks a lot!
Glad you enjoyed it!
Sir, What is a helper method?
Basically a method that takes a small tasks and puts it into it's own method. Even tho this is just a 30 second preview, the video description and transcript should give you an idea: teamtreehouse.com/library/helper-methods
can you make a video on helper method please.
Hi, Can you provide a starter project along with full source code.
That's a good idea, Aditya. I'll start doing that.
If this is real life situation then IF statements would be changed for GUARD, right? Is there a practical difference between those two ?
It would depend on the situation. The main reason you would use GUARD is if you wanted to exit the function as a result of the "failed" guard. Guards provide an "early exit" from the function if you want it.
Hey Sean why we use " static var"
Here's a great, concise explanation: www.hackingwithswift.com/read/0/18/static-properties-and-methods
Thanks
why there is a double if statement, why if else is not use instead ?
Hi Sean!
Have a question:
In case - we don't need error (wich in catch block) - can we use something like:
let isLoggedIn = try? login() ?? false
(login() returns true in case of success)?
I'm not sure of the exact code you're looking for, but in general, if you're not going to handle the error then you don't need to full do, try, catch block;
Actually - in case of some code throws an error - you have to deal it with do try catch block.
The question I'm sure many have is: why use this instead of *if / else* statements? What is the actual advantage of using Do Try Catch?
When an iOS job applicant is at an onsite interview are they generally allowed to bring a paper notebook or iPad to use as a reference during the coding exercises? (algorithms excluded)
I've never seen that allowed.
sean, when variable looks like yours : var isValidEmail : Bool { ..... return ..} Is this a computed property or what?
Yup, that's a computed propery
@@seanallen Anyway, do you think that trying to get into ios carreer is a good idea in present times? I want to try send come resume in next month in my country(Poland). I don't know how market look like. Unfortunately i don't see so many offers;/ at least in my area
Beautiful !
Thanks Radoslav!
using api is not that easy i am stuck on it can you explain it for us as i am a beginner
I have a video coming out in a few weeks called "Network Calls for Beginners". Keep an eye out...
@@seanallen thanks in advance
hello, you variable isValidEmail : bool , then put the { } , what is this, you add a closure behind it, so it will be executed everytime ? sorry i am a newbie, see this sometimes, don't know wut it is officially called ?
This is called a "computed property". Which means the property isn't a set value, like for example "var myNumber = 1". In that case, "myNumber" is always 1 (unless you change it). In a computed property, the value needs to be figured out. It's figured out by whatever logic you write in the {}
thank you, very clearly explained.
imo it would be better to handle the title and message of the error inside the error enum, that way you can just call the alert message once. Nevertheless, probably the best video I've come across on error handling.
Good point. Thanks for the kind words, Luke!
Can you give me an example of how would you pass both title and message with enum value?
I can add an associated rawValue, but only of a simple type (Int, String, Bool), but not a dictionary like: [String:String]
Here is the only thing I could come up with, which is a bit ugly, imho:
enum LoginError: String, Error {
case incompleteForm = "Incomple Form:Please fill out both email and password fields"
case invalidEmail = "Invalid Email Format:Please make sure you format your email correctly"
case incorrectPasswordLength = "Password Too Short:Password should be at least 8 characters"
}
do {
try login()
// Transition to next screen
} catch let error as LoginError where [.incompleteForm, .invalidEmail, .incorrectPasswordLength].contains(error) {
let errorMessage = error.rawValue.split(separator: ":")
Alert.showBasic(title: String(errorMessage[0]), message: String(errorMessage[1]), vc: self)
} catch {
Alert.showBasic(title: "Unable To Login", message: "There was an error when attempting to login", vc: self)
}
I'm also curious about this question. If Mr. Allen can enlighten us it would be great.
You must use tuples, and you can man.
The thumbnail looked like a Nike commercial lol (do... try... catch)
Awesome!
Great content, but feel like they should be supplemented with examples of how they are applied within the context of iOS development similar to this video
What is really confusing about Swift syntax here is the implicit `error` inside the catch block, when you might already have an error defined above.
great
Thanks!
Have a question about the Do, Try, Catch syntax? Leave a question below!
Hey Sean, you mentioned that we use do,try,catch in cases where the function outputs some error. What is the advantage of using do,try, catch over an "if else" in this case. Is it that it makes the code looks better of is there some benefit in actual speed of execution or something like that ?
Sean, what your methodologies when coding? I feel that some times I get lost in code because I don't have a general sense of direction when a problem may arise. I'm trying to get better, typically would write python scripts but now that I am getting into this type of development and using more and more files such as classes to fulfill the application its hard planning. It can kinda be discouraging but I feel like its something common and all you need is the right sense of direction when stuck.
I know what you're saying. It was overwhelming at first. To be honest, there's not really an easy answer. It's something I've developed over a couple years, and I'm still building. (Talking about my methodologies). They are constantly evolving and improving. Coming up with how you like to structure your apps is something that comes with time an experience.
bro i have a request that i feel that once in a week for an hour you should interact with us for our queries or doubt if you can give one hours of your precious schedule it will be pleasure for us
waiting for yours zoom , google meet link
This video has some gems 0:44, 1:55, 10:15 and maps this up to the GUI here: 12:18 and as an added bonus "Feel free to steal this:" 11:39
Thanks for the kind words, Gjermund.
Thanks for your video.
I wih to ask one thing:
How can I implemente an error handling in the code below?
Presently it doesn't work because it doesn't throws errors.
It gives these warnings, in xcode:
No calls to throwing functions occur within 'try' expression
and
'catch' block is unreachable because no errors are thrown in 'do' block
Thank you for any help, if possible.
DispatchQueue.main.async {
do
{
self.name.text = MyAccountViewController.nameofuser
self.email.text = MyAccountViewController.email
self.sessions.text = "\(MyAccountViewController.active_device_sessions)/\(MyAccountViewController.max_devices_sessions)"
self.plan.text = MyAccountViewController.userplan
try MyAccountViewController.used_disk_space = ByteCountFormatter.string(
fromByteCount: Int64(MyAccountViewController.used_disk_space)!,
countStyle: ByteCountFormatter.CountStyle.file
)
try MyAccountViewController.max_disk_space = ByteCountFormatter.string(
fromByteCount: Int64(MyAccountViewController.max_disk_space)!,
countStyle: ByteCountFormatter.CountStyle.file
)
try self.space.text = "\(MyAccountViewController.used_disk_space)/\(MyAccountViewController.max_disk_space)"
}
catch let e {
debugPrint("failed to get user data: \(e)")
}
}
Hard to say just by looking at the code in a RUclips comment. I'd have to play around with it in Xcode to debug it.