swift3转swift5


Swift 5 has released and is available with Xcode 10.2. Today we’ll be discussing what it brings to the table.

Swift 5已发布,可用于Xcode 10.2。 今天,我们将讨论它带来的好处。

(Swift 5 Features)

(1. ABI Stability)

Swift 5 is ABI Stable!

Swift 5是ABI稳定版!

What is ABI Stability?
ABI stands for Application Binary Interface.
It enables binary compatibility between apps and libraries compiled with different Swift versions.
So far every iOS application ships its own Swift version. This is because Swift isn’t available as a part of the iOS itself.

什么是ABI稳定性?
ABI代表应用程序二进制接口。
它使应用程序和使用不同Swift版本编译的库之间具有二进制兼容性。
到目前为止,每个iOS应用程序都附带自己的Swift版本。 这是因为Swift无法作为iOS本身的一部分使用。

From Swift 5, ABI stability would allow future swift versions to be binary compatible with Swift 5.

从Swift 5开始,ABI的稳定性将使以后的Swift版本与Swift 5二进制兼容。

Hence we won’t have to ship Swift with our applications anymore. This will reduce the bundle size.
Also, there will be compatibility between applications and libraries that may have been compiled with different Swift versions.

因此,我们将不再需要将Swift与我们的应用程序一起交付。 这将减小捆绑包的大小。
另外,可能已经用不同的Swift版本编译的应用程序和库之间将具有兼容性。

Generally, when discussing language features we fire up the Playground in our Xcode. You can do the same!

通常,在讨论语言功能时,我们会在Xcode中启动Playground。 你也可以做到的!

(2. Raw Strings Literals)

Escaping backslashes and quotes can always get frustrating.
With the new Raw String Literals, padding the strings with a # treats the quotes as they are and pads the slash with another.

转义的反斜杠和引号总是令人沮丧。
使用新的Raw String Literals,用#填充字符串会将引号照原样使用,并将斜杠替换为另一个。

The following code explains what we mean:

以下代码解释了我们的意思:

let regularString = "\\Hello \\Swift 5"
let rawString = #"\Hello \Swift 5"#

let rawQuote = ###"We have no "limits"###


ios swift 刷新 swift5支持ios版本_ios swift 刷新

Swift 5 Raw Strings

Swift 5原始字符串



As you can see we don’t need to escape the quotes.

如您所见,我们不需要转义引号。

In order to make escaping work with the above syntax, we need to pad the backslash with a # as \#(). It changes the syntax of string interpolation slightly as shown below:

为了使转义能够使用上述语法,我们需要在反斜杠上加上#作为\#() 。 它会稍微改变字符串插值的语法,如下所示:

let name = "Journaldev"
let greeting1 = #"Hello, \#(name)!"#
let greeting2 = #"Hello, \(name)!"#




Swift Raw String Backslash

Swift原始字符串反斜杠



let regex1 = "\\\\[A-Z]+[A-Za-z]+\\.[a-z]+"

let regex2 = #"\\[A-Z]+[A-Za-z]+\.[a-z]+"#



(3. Integer Multiples)

Before swift 5:

在快速5之前:

let myNumber = 4

if myNumber % 2 == 0 {
    print("number is multiple of 2")
}

Now we have the isMultiple(of:) method:

现在,我们有了isMultiple(of:)方法:

4.isMultiple(of: 2)
6.isMultiple(of: 3)

(4. Flatten Optional try?)

Earlier try? used to give us nested optional like String?? or Int??.
In Swift 5, try?.someExpr() behaves the same way as foo?.someExpr()

较早尝试? 曾经给我们嵌套的可选String??String??Int??
在Swift 5中, try?.someExpr()行为与foo?.someExpr()

//Swift 4.2 : String??
//Swift 5 : String?
let messages = try? person?.getName()

(5. Result Type)

Result Type is a much-needed feature. It forces you to handle success and error parts of a response/result gracefully before accessing the actual value.

结果类型是非常需要的功能。 它迫使您在访问实际值之前适当地处理响应/结果的成功和错误部分。

enum Result {
    case value(Wrapped)
    case error(Failure)
}

Example:

例:

let result = Result { try String(contentsOfFile: "xyz") }


switch result {
case .success(let string):
    print(string)
case .failure(_):
print("failure")
}

The above snippet prints failure.

上面的代码段打印失败。

Result Type is useful in URL and async requests!

结果类型在URL和异步请求中很有用!

(6. Handle Future Enum cases)

Swift requires switch cases to be exhaustive.
One way to deal with this is by using a default case.
The default case handles all the cases for which you want to handle commonly.

Swift要求切换案例要详尽无遗。
解决此问题的一种方法是使用默认情况。
默认情况下会处理您要共同处理的所有情况。

Future Enum cases has a syntax @unknown default: It is used only instead of default case.
Whenever a new enum is added, this case indicates you with warning, whether you want to handle that enum case separately.

未来的枚举案例的语法为@unknown default:
仅用于代替默认情况。
每当添加新的枚举时,这种情况都会警告您,是否要单独处理该枚举情况。

(7. Compact Dictionaries)

Instead of using map, reduce and filter to filter out nil or odd values, Swift 5 provides with:
compactMapValues(_:)

Swift 5提供了以下功能,而不是使用map,reduce和filter来过滤出零或奇数:
compactMapValues(_:)

It can be used as:

它可以用作:

let students = ["Anupam": "10", "Chugh": "ten"]
let mapStudents = students.compactMapValues(Int.init)
print(mapStudents)

let dictionary = ["A" : 1, "B" : 2, "C": nil]
let output = dictionary.compactMapValues{$0}
print(output)

The output in the playground for each line is”

操场上每条线的输出是“




ios swift 刷新 swift5支持ios版本_ios swift 刷新_02

Swift 5 Compact Values

Swift 5紧凑值



(8. @dynamic callable)

Swift extends its interoperability to other languages like Python, JS by using dynamic callables.

Swift通过使用动态可调用对象将其互操作性扩展到其他语言,例如Python,JS。

An example from the docs shows how to :

文档中的示例显示了如何:

class Dog:
    def __init__(self, name):
        self.name = name
        self.tricks = []  # creates a new empty list for each `Dog`
        
    def add_trick(self, trick):
        self.tricks.append(trick)

Now to call these in Swift, we simply need to do:

现在在Swift中调用它们,我们只需要做:

//import DogModule.Dog as Dog
let Dog = Python.import.call(with: "DogModule.Dog")

// dog = Dog("Brianna")
let dog = Dog.call(with: "Brianna")

// dog.add_trick("Roll over")
dog.add_trick.call(with: "Roll over")

That’s a wrap on this tutorial on Swift 5 Features.

这是本教程有关Swift 5功能的总结。

翻译自: https://www.journaldev.com/27707/swift-5-features

swift3转swift5