Swift 2.0

Source: Internet
Author: User

1. Use of Guard

Guard is the Guardian consciousness, and it is similar to the use of If. When the expression after the guard is false, the content behind the else is executed. It is often used to judge a situation to be deliberately avoided. Such as:

Guard Let Somevalue = Valueforkey ("key"else  {    return} 

The above code uses KVC to get a value, and if it fails, it returns directly. The code above is equivalent to the following code, but the amount of guard code used is much less.

Let somevalue = Valueforkey ("key"if somevalue = = Nil {     return }

2. Exception handling

Swift's exception handling was introduced in swift2.0. We use it for exception handling in Cocoa Touch NSError . In the new Swift 2.0, we can use the new ErrorType protocol.

In Swift, the enum best way to build your own exception type is to confirm the new one in your enum ErrorType .

enum Myerror:errortype {    case  notexist  case  Outofrange}

2.1 Throwing Exceptions

If a function throws an exception, it needs to precede the function name with the Throw keyword, as

// has a return value ...} Func fun2 () throws {//  no return value    guard Let somevalue = Valueforkey ("key" /c8>Else  {        throw  myerror.notexist              }}

2.2 Handling Exceptions

Using the Do-catch statement to handle exceptions

 Do {    trycatch  myerror.notexist {    //  deal with not existcatch  myerror.outofrange {    //  dealwith not exist}

Receive all exceptions

 Do {    catch  _ {}

2.3 Defer

Defer is similar to try/finally in Java (there is no try/finally in standard C + +, but there is a digression in BCB), which allows us to execute the code that must be executed in the finally code block.

Unlike finally, defer is deferred execution, which executes the code in defer at the end of the block.

func dosomething () {print ("CheckPoint 2") Defer {print (" Clean up here")} print ("CheckPoint 3")}func checksomething () {print ("CheckPoint 1") dosomething () print ("CheckPoint 4")}checksomething ()//CheckPoint 1, CheckPoint 2, CheckPoint 3, clean up here, CheckPoint 4

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.

Http://www.jianshu.com/p/96a7db3fde00

Https://www.baidu.com/s?wd=C%2B%2B%E6%9C%89%20try%2Ffinally%E5%90%97&rsv_spt=1&issp=1&f=8&rsv_ bp=0&rsv_idx=2&ie=utf-8&tn=baiduhome_pg&rsv_enter=1&rsv_sug3=14&rsv_sug1=7&rsv_t= Bd91sdwft192hvmdq7yh2eyjf91%2bojhu376ifejovu15qrzo3f9piavfdkzzgaf3tp9o&rsv_sug2=0&inputt=4147&rsv_ sug4=4342

Swift 2.0

Related Article

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.