Some new features of Swift 3.1
Recommended order
This article comes from the contribution of the park study, introduces the new features of Swift 3.1, and thanks to the grant of parking. The following is the body of the article.
Body
Apple has finally released Xcode 8.3 and Swift 3.1. If you don't have time to read through the release note carefully, at least some of the new features in Swift 3.1 are worth knowing, so we've created a free content series. Of course, Swift 3.1 and Swift 3 are compatible at the source level, so if your project has been updated to Swift 3, this update should not cause you too much trouble. However, Xcode 8.3 removes the support for Swift 2.3, so if you're still on the earlier version of Swift, it's good to be updated with caution.
Next, let's briefly introduce the content in this series.
SE-0080 failable Initialize of numeric types
This is an improvement brought about by SE-0080. For example, before, when we put aDoubleForced to convert intoInt, Swift will automatically cancel the part after the decimal point. This is useful when we need to perform a "lossless" precision conversion between different number types, and when transformation leads to loss of precision,init(exactly:)Method will give us a returnnil。 We show you the specific use of this feature through one of the most common scenarios: parsing the JSON returned by the server.
SE-0045 of the two new filter elements added in sequence
prefix (while:)anddrop (while:)is a new addition tosequencetype of two API methods, through which can help us to write more effectively in
SE-0103 temporarily converted to escaping closure
As described in SE-0103, in Swift 3, the function's closure type parameter is changed from escaping to non-escaping by default. This is well understood, since most of the closure parameters used for functional programming do work in a non-escaping way.
But this proposal also mentions the problem that sometimes we need to pass the closure of the non-escaping attribute to a function that requires the closure of the escaping attribute. When do you need this kind of scene? Perhaps the explanation you see elsewhere is that this is a less common use.
SE-0141 constrained Swift version via available
Although we can achieve version control through the form of # If, we have a less convenient place to write libraries in this way. Each swift version that the compiler wants to support is compiled independently. So, if multiple swift versions are compatible between a swift ABI cumulative update, it is a cumbersome task to carry a library of these days. A better approach, of course, should be to compile only once, and then build the library containing the swift versions that each API can support.
For this reason, the Swift 3.1 has@availablebeen extended to allow us not only to constrain the operating system, but also to specify the version of Swift.
SR-1009 using representational type constraint generic parameters
In Swift 3.0, if we are going to be a specific type ofOptionalAdd toextension, not a very easy thing to do. We have to useprotocolConstrains a computed property of the same type to simulate a constraint of a representational type. But in Swift 3.1, this flaw is made up, and we can not only use the generic parameterprotocolConstraints, you can also use the representational type as a type constraint, which is somewhat like a template-biasing technique in C + + generics.
SR-1446 two improvements to inline types
In this section, let's talk about topics related to inline types. In Swift 3.1, there are two improvements to the inline type:
-
An inline type of a normal type can use a generic parameter of its outer type directly, at which point it is still a normal type;
-
An inline type of a generic type can have a generic parameter that is completely different from its peripheral type;
Click "Read original" To view the free video tutorials in the park. If you would like to subscribe to the service of parking, you can use this link to get 30% members longer: Http://t.cn/R6GtgJS.
Some new features of Swift 3.1