New Features of swift2.0 and swift2.0

Source: Internet
Author: User

New Features of swift2.0 and swift2.0
More articles follow the blog www.goofyy.com/bolg/ Portal: goofyy Technology House

Swift language is a new development language launched by Apple at the WWDC Developer Conference. In the past year, swift has undergone several changes, apple launches swift official version 2. 0. and open source code at the end of 2015, I have to say that this is a very tricky thing. In fact, the significance of swift open source is for developers, the main reason is that you can use the swift development language to develop applications on more platforms. I have to say that Apple has also made a big move.

Xiao Bian also dared to treat himself as a lover of the swift language. After several months of study, I have to say that the swift development language makes the editor feel the pain. Let's take a look at the updates made by swift2.0 compared with swift1.2.


1. Error Handing


Swift2.0 introduces a new error handling mechanism, which is new. In fact, it is relatively mature among other languages. keywords such as cathch throw try, in java, python, and other languages, A mature error handling mechanism has already been formed. Before July 2.0, swift did not introduce error handling similar to java, but adopted NSError. I have also seen an article on swift language error handling in the forum. However, swift is dedicated to handling new errors in swift2.0. Not much nonsense. About New error handling



The new ErrorType protocol introduced by swift2.0 produces errors. throw must comply with the ErrorType protocol to throw errors. Here we use an enumeration type to list the causes of errors.

enum errorPlayGame:ErrorType {    case noMoney    case noTime    case noFriendToGether}
Here, the enumeration type errorPlayGame complies with the ErrorType protocol, so that errors can be thrown normally.


For example. I want to play games with my friends, but I don't know if I have time, money, or someone to accompany me on that day. Then we write the program as follows:


// Define the enumeration type errorGame to comply with the ErrorType protocol enum errorGame: errorType {case noMoney case noTime case noFriendToGether} let money = 0let time = 23let numOfMyFriend = 2 // define a method to play games func PlayGame () throws {guard money> 0 else {throw errorGame. noMoney} guard time> 0 else {throw errorGame. noTime} guard numOfMyFriend> 0 else {throw errorGame. noFriendToGether} // defines a method for getting started to play and calls PlayGame. Catch exception. Func startGame () {do {try PlayGame () print ("starting Game")} catch errorGame. noMoney {print ("no money")} catch errorGame. noTime {print ("no Time")} catch errorGame. noFriendToGether {print ("no friend play Game with me")} catch {}} startGame ()
The money here is less than 0, so the program will throw an exception, and the program idea is easy to understand. Let's talk about some points of attention. In the startGame method, do {} is to start executing the program, an error occurred while capturing the try file. If the task is successfully executed, "starting Game" is printed ". After the three errors in cathch enumeration are completed, the catch should be closed, that is, an empty catch should be added at the end, otherwise, Error throw from are not handled because the enclosing catch is not exhaustive indicates that the catch is not closed.


The PlayGame method has such a keyword guard. In fact, gaurd serves as a guard. For example


guard numOfMyFriend > 0 else {        throw errorGame.noFriendToGether    }
The following program can be executed only when numOfMyFriend> 0. Otherwise, an exception is thrown. Like Uncle Zhang said, is there a card? Cards can be used to enter the door.



2. Bind Binding



guard let pants = pants, frog = frog else {  // sorry, no frog pants here :[  return} // at this point, frog and pants are both unwrapped and bound!


The new gaurd syntax allows you to build an early exit point in a code cycle. This makes the code clearer.


3. Protocol extended Protocol Extensions

Protocol Expansion has to be said to be a good idea. Protocol expansion was added in the new swift2.0. The previous expansion mainly expanded the existing class, struct and enumeration types and its methods, the new swift syntax can expand the protocol. For example.



Protocol Speed {func 100km acceleration time ()-> Double} struct Cadillac: Speed {var price: Int func 100km acceleration time () -> Double {return 1.5} let Cadillac 1 = Cadillac (price: 20000000) Cadillac 1. 1.5 km acceleration time () // At this time I want to get the ranking of the card category, for example, 1.5 ranking 100 * extension Speed {var ranking: int {get {return Int (100 km acceleration time () *)} Cadillac 1. ranking


Here, the ranking function for protocol expansion is added,

Cadillac 1. Ranking output ranking


4. Validity Check


This is mainly to be compatible with some old ios versions. The main function is to check whether some new APIs are applicable in some old versions, including OS x, IOS, and Watch OS software development.


If # available (iOS 8.0, OSX 10.10, *) {// use Handoff APIs when the version matches. let activity = NSUserActivity (activityType: "com. example. shoppingList. view ") activity. becomeCurrent ()} else {print ("this API is unavailable ")}


5. do... while loop rename

Do... while loop is changed to repeat... while loop, mainly to distinguish it from do in the previous error handling, it is also more intuitive,


var j = 1repeat { j++print(j)}while(j <= 10)
More articles follow goofyy's website.


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.