官术网_书友最值得收藏!

  • iOS Programming Cookbook
  • Hossam Ghareeb
  • 248字
  • 2021-07-09 18:29:45

How it works...

We started our code example by creating a new error type called SignUpUserError, which conforms to ErrorType protocol. As we see, we listed four errors that may happen while signing up any user in our system, such as invalid first name or last name, invalid e-mail, weak password, and passwords that don't match. So far, so good!

Then, we create a function signUpNewUserWithFirstName, which takes user input values, and as we can see, we have marked it with the throws keyword. The keyword throws says that this function may throw an error anytime during execution, so you be prepared to catch errors thrown by this method.

Inside the implementation of the function, you will see a list of guard statements that checks for user input; if any of these guard statements returned false, the code of else statement will be called. The statement throw is used to stop execution of this method and throw the appropriate error based on the checking made.

Catching errors is pretty easy; to call a function that throws error, you have to call it inside the do-catch block. After the do statement, use the try keyword and call your function. If any error happens while executing your method, the block of code inside the catch statement will be called with a given parameter called error that represents the error. We've created a switch statement that checks the type of error and prints a user-friendly statement based on the error type.

主站蜘蛛池模板: 武山县| 武山县| 汉阴县| 三门峡市| 西和县| 余姚市| 安阳县| 会东县| 贵阳市| 沙田区| 澜沧| 大安市| 曲沃县| 泰和县| 集贤县| 广德县| 金寨县| 西城区| 定襄县| 台北市| 安多县| 万安县| 屯门区| 莱州市| 宣武区| 日照市| 西乌| 永新县| 云南省| 乐业县| 昭苏县| 新平| 普定县| 清水县| 长乐市| 霸州市| 九龙坡区| 衡东县| 同江市| 卓尼县| 上高县|