In the blink of an eye, Swift is already over a year old. This fresh, stylish, type-safe, and faster-executing language has gradually gained popularity among developers. I also love this new programming language very much. In June this year, the annual WWDC conference came as scheduled. At the conference, Apple released Swift 2.0, which introduced many new features to help developers build applications faster and easier. Here I will also talk about the new features in Swift 2.0 that are worth your attention. guard Statement The guard statement is similar to the if statement in that it determines what to execute next based on the Boolean value of the expression after the keyword. However, unlike the if statement, the guard statement only has one code block, unlike the if statement which can have multiple code blocks such as if else. So what is the role of the guard statement? As the name suggests, it is a guard. The guard statement will execute the code in the following code block only when the Boolean value of the expression after it is false. If it is true, the entire guard statement will be skipped. Let's take an example to see. Let's take this year's college entrance examination as an example. When entering the examination room, the ID card and admission ticket are generally checked. We write a method like this:
The first guard statement in the above code is used to check the ID card. If the ID card is not brought, that is, the expression is false, the code in the curly brackets is executed and returned. The second guard statement checks the admission ticket. If both certificates are complete, the last print statement is executed, and the codes in the curly braces of the two guard statements above will not be executed because the Boolean values of their expressions are both true. It is worth noting here that id and examNumber can be used outside the guard statement, that is, after guard verifies its expression, id and examNumber can be used in the scope of the entire method and are unpacked. Let's write a similar method using an if else statement:
We can see that the method implemented by if else is obviously not as accurate as that implemented by guard. Moreover, the scope of id and examNumber is limited to the first curly brace of if. If it exceeds this scope, the compilation will report an error. It is not difficult to see from the above two small examples that the guard statement is like a competent guard, which checks layer by layer to prevent anything that is not allowed to happen, and makes the code more readable, which is great. Exception handling In the Swift 1.0 era, there was no exception handling and throwing mechanism. If you wanted to handle exceptions, you either used if else statements or switch statements to judge and handle them, or used closure callback functions to handle them, or used NSError to handle them. None of the above methods can handle exceptions as smoothly and leisurely as the try catch exception control statement in Java, and they will also reduce the readability of the code. When Swift 2.0 came, everything was different. In Swift 2.0, Apple provides an exception control mechanism consisting of five keywords: throws, throw, try, do, and catch. Let's take an example to see how to use it. I'll use the example of using a mobile phone to browse Moments. First we need to define the exception enumeration. In Swift 2.0, Apple provides the ErrorType protocol that our custom exception enumeration needs to follow:
We define the error enumeration 'wechatError' that causes WeChat to fail to be swiped. Then define a method checkIsWechatOk() to check whether WeChat can be swiped:
Note that there is a throws keyword after the method name, which means that the exception generated by this method is thrown to the upper layer. Use the guard statement in the method body to judge various states, and then use the throw keyword to throw the corresponding exception. Then we define the method of swiping WeChat:
In the above code example, the try keyword is used before the method that checks whether WeChat can be swiped, indicating that the method is allowed to throw exceptions. Then the do catch control statement is used to capture the thrown exception and perform related logical processing. This exception handling mechanism makes Swift more comprehensive and safer, and improves the readability of the code, which is great. Protocol Extensions In the Swift 1.0 era, a protocol is basically like an interface, defining several properties and methods for classes, structures, and enumerations to follow and implement. In Swift 2.0, you can extend the properties or methods of a protocol, similar to extending classes and structures. This opens the chapter of protocol-oriented programming. In Swift, most basic objects follow the CustomStringConvertible protocol, such as Array and Dictionary (Printable protocol in Swift 1.0). This protocol defines a description method for printing objects using the print method. Now we extend this protocol with a method to print the content in uppercase:
If we want to achieve the effect of the above example in the Swift 1.0 era, we need to extend Array and Dictionary respectively. Therefore, the extension of the protocol greatly improves our programming efficiency and also makes the code more concise and easy to read. Changes to print statements In Swift 1, there are two methods for printing statements in the console, 'println()' and 'print()'. The former prints on a new line, while the latter prints on a continuous line. In Swift 2, 'println()' has become a thing of the past, and has been replaced by a combination of the two. If you want to print on a new line, you now need to write:
Available Check As an iOS developer, everyone wants to use the latest version of iOS API for development, which saves time and effort. However, this is often not the case, because we often need to adapt to older versions of iOS, which will face a problem. Some new features or classes cannot be used in older versions of iOS, so during the coding process, we often judge the iOS version, like this:
The above is just one way. Before Swift 2.0, there was no standard mode or mechanism to help developers determine the iOS version, and it was easy to make mistakes. After the arrival of Swift 2.0, we have a standard way to do this:
This feature makes us so happy. do-while statement rename The classic do-while statement has been renamed to repeat-while:
I personally feel it is more intuitive. The defer keyword In some languages, there are control statements like try/finally, such as Java. This statement allows us to execute the code that must be executed in the finally code block, no matter what happened before. In Swift 2.0, Apple provides the defer keyword, which allows us to achieve the same effect.
In the above example, we can see that after printing "CheckPoint 2", "Clean up here" is not printed, but "CheckPoint 3". This is the role of defer, which delays print("Clean up here"). Let's look at another I/O example:
The above example is a pseudo code of an I/O operation. If the ioStatus obtained is normal, then there is no problem with the method. If the ioStatus obtained is an error, it will be caught by the guard statement and the return operation will be executed. In this case, closeFile(file) will never be executed, and a serious bug will be generated. Let's see how to use defer to solve this problem:
We put closeFile(file) in the defer code block, so that even if ioStatus is error, the code in defer will be executed before return is executed, which ensures that no matter what happens, the file will be closed in the end. Defer is another feature that ensures the robustness of our code, and I like it very much. Of course, there are more new features in Swift 2.0 than those listed above, but a glimpse of the whole picture shows that Swift 2.0 strives to be faster and safer to the extreme, which is good news for developers. Let us enjoy this wonderful language to the fullest. |
<<: Hprose for Node.js 1.6.0 released
>>: Example analysis: Performance optimization of "Qichu Encyclopedia"
When the Internet becomes a traditional industry,...
[[137024]] When you are used to deciding what you...
Ajie·How to break the black label of Pinduoduo tr...
We all know that how many people can be promoted ...
Many friends complained during the flood season, ...
gossip "Herpes zoster, herpes zoster, can ki...
User operation is an invisible skill that many ne...
Lysergic acid diethylamide, also known as "l...
Why do some people’s answers on Zhihu always have...
Recently, foreign media reported that "Hyund...
Is the Yellow River going to be renamed? This riv...
The oil and gas come from underground to the well...
This article mainly discusses the analysis of use...
The reason why Jay Chou's songs are timeless ...
For any product design, the construction process ...