iOS development-new features Swift article &swift 2.0 exception handling

Source: Internet
Author: User

Swift 2.0 Exception Handling

WWDC 2015 announced the new Swift 2.0. This major update provides Swift with new exception handling methods. This article will be discussed mainly around this aspect.

How do I build an exception type?

In IOS development, we face a lot of exception handling. In Cocoa Touch we use Nserror for exception handling. In the new Swift 2.0, we can use the new ErrorType protocol.

In Swift, enum is the best way to build your own type of exception, and you just have to confirm the new ErrorType in your enum.

Enum Myerror:errortype {case notexist case Outofrange}

How to throw an exception

Before throwing an exception, we need to use throws to indicate that an exception will be thrown before the return arrow of the function or method.

 1 func mymethodretrunstring ()throws-2/  No Return, we can just add thro WSin the end3 func mymethodretrunnothing () throws

After the declaration, we need to throw an exception in the function or method, it is easy to use throw.

1 func MyMethod () throws2   //...3   //item is an optional value4Guard Let item = ItemElse {5 //need throws the error out6 Throwmyerror.notexist7   }8   //Do with item9}

The above code uses guard for unwrap optional value. This is a new approach provided by Swift 2.0.

Guard

There are already guard in languages like Haskell, Erlang, and more about it here.

Guard translation comes to be understood as guardian, guard.

In Swift, Guard is a bit like if but they have two very important differences

    • Guard must enforce an else statement

    • The code after the guard will run only if the condition of the Guard review is true (like a guard, the condition does not allow the past).

The else in guard can only execute conversion statements, such as return, break, continue, or throws. Of course you can also return to a function or method here.

It is important to note that the use of guard will improve the readability of your code, but it also means that your code will be executed in a very clear order, which needs to be handled by developers.

Although we refer to guard in exception handling, it does not mean that it can only be used in exception handling. It has a wide range of applicability, perhaps through the array I will specifically for the use of Guard to write an article.

How do I get and handle exceptions?

The above describes how to build throw exceptions, and getting and handling exceptions is easy. Use the Do-catch mechanism.

1  Do {2try  functionwillthrowerror ()3   Catch {4//  deal with error5   }

The do-catch mechanism is easy to understand. Many programming languages also use a similar mechanism for exception handling, but there is a more important feature in Swift.

Catch and switch have the same ability to Pattern Matching. So, with catch you can do more advanced processing of the exception's parsing

1  Do {2try  functionwillthrowerror ()3   Catch myerror.notexist {4//  deal with not exist5   Catch Myerror.outofrange {6//  deal with not exist7   }

It's worth mentioning. An improvement in Swift 2.0 that is not related to exception handling

The Do-while cycle is not in Swift 2.0 and is replaced by Repeat-while. Apple said the change was made to improve the readability of the code. But I think it's more comfortable for us to use Do-catch.

Do not handle exceptions

If I don't want to deal with exceptions, or I'm pretty sure that a method or function will throw an exception while declaring it, I know for sure that I will never throw any exceptions when I use them. In this case we can use the try!

 1try! Functionthrowerrornil ()

Of course, if you use try!, and your method or function throws an exception, then you get a running exception (runtime error) so we developers need to use caution.

Defer

We'll talk about defer before the end of the article.

Before your code block is about to end. If you use the defer. The code in it will run. equals to say, gives you the last chance to do some processing. If you are familiar with BDD or TDD, then you can refer to their aferall mechanism

1 func myFunction () throws {defer {2 //No matter what happened I need do something3Print"All do , clean up here")4   }5Guard Let item = ItemElse {6 //need throws the error out7 Throwmyerror.notexist8   }9Guard Item.count > MaxNumberElse {Ten //need throws the error out One ThrowMyerror.outofrange A   } -   //Do something with item -   // ... the}

Note that if you have multiple defer statements, they will be executed in the same order as the stack, the last one in, and the first one out.

Summarize

    • Use ErrorType Help to build your exception type

    • Use throws to declare exceptions, throw exceptions with throw

    • Using the do-catch mechanism to get and handle exceptions

iOS development-new features Swift article &swift 2.0 exception handling

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.