With the recent release of Swift 3.1, let’s explore what changes it has made.
The Sequence protocol adds two methods
protocol Sequence {
// ...
/// Returns a subsequence by skipping elements while `predicate` returns
/// `true` and returning the remainder.
func drop(while predicate: (Self.Iterator.Element) throws -> Bool) rethrows -> Self.SubSequence
/// Returns a subsequence containing the initial elements until `predicate`
/// returns `false` and skipping the remainder.
func prefix(while predicate: (Self.Iterator.Element) throws -> Bool) rethrows -> Self.SubSequence
}Copy the code
The drop method will iterate over all the elements and accept a predicate that returns true for all the elements of the predicate, generating a new array and returning.
The prefix method is also useful. It accepts a predicate until the predicate returns false, ignores all subsequent elements, generates a new set and returns. Prefix = prefix
This change comes from Proposition SE-0045, original address: github.com/apple/swift…
Swift version compiler compatibility syntax support
Because Swift versions change very quickly, and the library interface often changes a lot, Swift has added an extension to the @available syntax. Now you can specify the language version of your code, for example:
@available(Swift, Obsoleted: 3.1) class Foo {//... }Copy the code
The above code means that this class is valid only for compilers below Swift 3.1, not later versions. Obsoleted means to be removed from this version. Personally, I think this feature is very useful, especially for those of you who have experienced the failure of your project after the Swift version update.
This improvement stems from proposal SE-0141: github.com/apple/swift…
Although you could have used the #if swift(>= N) syntax to determine the swift version before, this is a compiler preprocessing command, so you need to build a version for each swift version of the compiler. (If you think about build preprocessing, it’s easy to understand why build for each version.)
Accurate numerical conversion
Swift 3.1 provides a concise implementation for conversion of numeric types, such as:
Let num = Int(exactly: 2.2) print(numCopy the code
This code up here prints nil instead of 2.
Why provide exactly this implementation, my interpretation is this. Because the Int constructor is passed a float of 2.2, it will become 2 if it is converted to Int. This may not be what you expect in some cases, such as when you parse JSON data sent from the server.
This interface will return nil if it finds that converting from floating-point to integer results in a loss of precision. Again, the following code will convert normally, because there is no loss of precision:
Let num = Int(exactly: 2.0) print(numCopy the code
This change comes from proposal SE-0080: github.com/apple/swift…
The end of the
Those are the major changes for Swift 3.1. This is a small release, so there won’t be too many language changes. In addition to improvements to the language itself, there are also improvements to package management tools.
For the full content, please refer to the official release document: swift.org/blog/swift-…
If you find this article helpful, you can also follow the wechat official account Swift-cafe and I will share more of my original content with you