New features of swift2.0 language

Source: Internet
Author: User

More articles focus on the blog www.goofyy.com/bolg/Portal: Goofyy Technology Residence

The Swift language is the new development language that Apple introduced at the 2014 WWDC developer conference, and by the year, Swift has undergone several changes, and in 2015 the new past developers ' meeting Apple launches the SWIFT Official version 2.0. And at the end of 2015 open the source code, have to say, this is a very pillbox pillbox thing, in fact, the meaning of Swift open source for developers, mainly can use the swift development language on more platforms to develop the application, I have to say, Apple also put a big trick.

Xiao Bian also ventured to be a fan of swift language. A few months of study, I have to say, swift development language Let Small series feel is Tongkuailinli. No nonsense, here's a look at the updates made by swift2.0 relative to the swift1.2 version.


1. Error handling errors handing


Swift2.0 introduced a new error handling mechanism, said is new, in fact, in other languages, has been relatively mature, cathch throw try and other keywords, in Java,python and other languages, has formed a relatively mature error handling mechanism, Swift did not introduce Java-like error handling before 2.0, but instead used nserror error handling. Earlier in the forum also saw an article about Swift language error handling, but Swift is also considered a conscious, in the swift2.0 added new error handling. Don't say much nonsense. Talk about the new error handling



Swift2.0 introduced the new ErrorType protocol, resulting in errors to be throw to comply with the errortype protocol , you can throw the error, here we use an enumeration type, List A cause of error

Enum Errorplaygame:errortype {case    Nomoney case    notime case    Nofriendtogether}
The enumeration type here Errorplaygame complies with the ErrorType protocol, and the resulting error can normally throw


Give me an example of a simple answer. I want to play games with my friends, but I don't know if there is any time, money or someone to accompany me on that day. Then we write the program as follows


//Define enumeration type Errorgame comply with ErrorType protocol enum Errorgame:errortype {case Nomoney case notime Case Nofriendtogether}let Money = 0let Time = 23let Numofmyfriend = 2//defines a way to play the game Func PlayGame () throws {Guard Mone Y > 0 else {throw Errorgame.nomoney} Guard time > 0 else {throw errorgame.notime} Gua Rd Numofmyfriend > 0 else {throw Errorgame.nofriendtogether}}//defines a start-to-play method that calls PlayGame. Catch exception. Func Startgame () {do {try PlayGame () print ("Starting Game")} catch Errorgame.nomoney {PR Int ("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, the idea of the program is easier to understand, to tell a few points of attention, Startgame method, do {} to do is to start executing inside the program, try to crawl the exception, if the execution succeeds, then print out "starting Game". Cathch after the enumeration of three errors, to close the catch, that is, to add an empty catch, otherwise the error will be error throw from was not handled because the enclosing catch was not Exha Ustive means that the catch is not closed.


There is such a keyword guard in the PlayGame method. This is actually simple and clear, in fact, the role of Gaurd is the role of a doorman, simply give an example


Guard Numofmyfriend > 0 else {        throw errorgame.nofriendtogether    }
Only Numofmyfriend >0 can execute the following program. Otherwise (else) throws an exception. Like the janitor said, is there a card? A card to enter the door.



2. Bind binding



Guard Let pants = pants, frog = Frog else {  //sorry, no frog pants here: [  return}//@ This point, frog and Pan TS is both unwrapped and bound!


Gaurd This new syntax allows you to build an early exit point in a code cycle . This way you can make your code clearer.


3. Agreement extension Protocol Extensions

Agreement expansion has to be said to be a conscience. The protocol extension was added in the new swift2.0, the previous expansion, mainly to the existing class, struct and enumeration types, its methods to expand, the new swift syntax can expand the protocol. Let me give you an example.



Protocol Speed {    func hundred km Acceleration Time () double}struct  Cadillac: Speed {    var price: Int    func hundred km Acceleration Time () Double {        Return 1.5    }}let Cadillac 1 = Cadillac (Price: 20000000) Cadillac 1. Hundred km Acceleration Time ()//At this point I want to get the Cadillac rankings, for example 1.5 on the ranking 1.5 * 100extension speed {    var rank : int {        get {            return Int (hundred km Acceleration time () * +)}}}    Cadillac 1. Ranking


This adds to the ability to rank the protocol extension,

Cadillac 1. Ranking  


4. Validity Check


This is primarily intended to be compatible with some older iOS versions. The main role is to verify that some of the new APIs are available in some older versions, including OS X,ios, Watch OS Software development



5. Do ... while loop renaming

Do.. The while loop changes to repeat. The while loop, which is also to be distinguished from the do of the previous error handling, is also more intuitive,


var j = 1repeat {J++print (j)}while (J <= 10)
More articles focus on Goofyy's website.


New features of swift2.0 language

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.