Swift 4 新增内容

作者: Lebron_James | 来源:发表于2017-06-06 23:29 被阅读623次

今天早上凌晨,苹果召开WWDC2017,想必大家都看了吧。新增的内容还是挺多的,特别是全新的CoreMLARKit,非常期待我们能用这两个framework能做出点什么新的东西,后续会为大家带来与这两个相关的一些文章。

另外Swift 4也发布了,我大概看了下官方的更新日志,我整理了可能会比较常用的主要几点,完整的更新日志请点击这里 >>

初始化一个多行字符串

可以直接在多行字符串中包含"

let quotation = """
The White Rabbit put on his spectacles.  "Where shall I begin,
please your Majesty?" he asked.
 
"Begin at the beginning," the King said gravely, "and go on
till you come to the end; then stop."
"""

可以直接在多行字符串中包含"""

let threeDoubleQuotes = """
Escaping the first quote \"""
Escaping all three quotes \"\"\"
"""

以下两个字符串是等价的:

let singleLineString = "These are the same."
let multilineString = """
These are the same.
"""

在字符串前后加空行,可以这样写:

"""
 
This string starts with a line feed.
It also ends with a line feed.
 
"""

如果一个多行字符串定义在一个方法中,实际字符串的值是不包含每一行前面的空格:

func generateQuotation() -> String {
    let quotation = """
        The White Rabbit put on his spectacles.  "Where shall I begin,
        please your Majesty?" he asked.
 
        "Begin at the beginning," the King said gravely, "and go on
        till you come to the end; then stop."
        """
    return quotation
}
print(quotation == generateQuotation())
// Prints "true"

如果把字符串改为下面的这个样子,第二行字符串前面的空格是不能忽略的:

func generateQuotation() -> String {
    let quotation = """
        The White Rabbit put on his spectacles.  "Where shall I begin,
                please your Majesty?" he asked.
 
        "Begin at the beginning," the King said gravely, "and go on
        till you come to the end; then stop."
        """
    return quotation
}
print(quotation == generateQuotation())
// Prints "false"

泛型下标 (Generic Subscripts)

下标可以是泛型的,并且可以包含where语句:

extension Container {
    subscript<Indices: Sequence>(indices: Indices) -> [Item]
        where Indices.Iterator.Element == Int {
            var result = [Item]()
            for index in indices {
                result.append(self[index])
            }
            return result
    }
}

上述代码的意思是:接受一个下标序列,并返回对应的元素组成的数组:

  • Indices必须遵循Sequence协议
  • indices必须是Indices类型
  • where语句要求Indices的元素必须是Int类型

协议组合中可以包含父类

例如下面这个例子:

class Location {
    var latitude: Double
    var longitude: Double
    init(latitude: Double, longitude: Double) {
        self.latitude = latitude
        self.longitude = longitude
    }
}
class City: Location, Named {
    var name: String
    init(name: String, latitude: Double, longitude: Double) {
        self.name = name
        super.init(latitude: latitude, longitude: longitude)
    }
}
func beginConcert(in location: Location & Named) {
    print("Hello, \(location.name)!")
}
 
let seattle = City(name: "Seattle", latitude: 47.6, longitude: -122.3)
beginConcert(in: seattle)
// Prints "Hello, Seattle!"

beginConcert方法中,location参数必须Location类型,并且遵循Named协议。

final关键字不能用在协议扩展中

在协议扩展里面,我们不能使用final

Swift 3中,在extension访问private属性会报错;而在Swift 4中,允许我们这么做。

1.jpg

Xcode 9可以自定义编译器的Swift版本

2.jpg

Xcode 9集成新的编译系统,编译速度更快

3.jpg

在Swift和OC混编时,我们要使用bridging headers,Xcode9可以预编译 bridging headers,大幅度减少编译时间,apple music的编译速度提高40%

4.jpg

在访问字符串的index时,不用再写value.characters.startIndex,直接写成value.startIndex即可

5.jpg

访问字符串的从中间某个index到endIndex的字符,可以省略endIndex

6.jpg

不允许在遍历数组的时候,修改当前遍历的数组

7.jpg

有任何问题,欢迎大家留言!

欢迎加入我管理的Swift开发群:536353151,本群只讨论Swift相关内容。

原创文章,转载请注明出处。谢谢!

相关文章

  • Swift 4 新增内容

    今天早上凌晨,苹果召开WWDC2017,想必大家都看了吧。新增的内容还是挺多的,特别是全新的CoreML和ARKi...

  • Swift访问控制权限

    概述 Swift 3中,新增了 fileprivate 和 open 权限,而在Swift4 中,对 filepr...

  • Swift 5 新增了什么?(下)

    书接前文(Swift 5 新增了什么?(上))继续介绍 Swift 5 的新增特性。 字符串插值更新 Swift ...

  • Swift 4 更新内容

    API更改 1. 字符串 (String) 1.1 本次更改消除了在String对象的String数组上迭代的问...

  • iOS 属性修饰词和权限控制词

    Swift 权限控制词 概述 swift3.0中,新增了fileprivate和open权限。swift4.0中,...

  • 更方便的单边Range

    安装Swift Toolchain 在开始这个系列的内容之前,你需要先安装Swift 4对应的toolchain。...

  • Swift语法笔记Part1

    Swift-day1内容提要 1.Swift介绍 2.Swift体验 3.常量 变量 重要 4.数据类型 5.逻辑...

  • Swift4-基础内容

    文章内容是根据https://www.cnswift.org/上面的学习内容来的,既是笔记,又是精简,记录的都是重...

  • Swift4基础内容

    类型 基本类型 Int Double Float Character String Bool SubString ...

  • MBProgressHUD的swift版本

    MBProgressHUD的swift版本,自己练习swift写的 新增一个MBSwiftHelper的单例,用来...

网友评论

    本文标题:Swift 4 新增内容

    本文链接:https://www.haomeiwen.com/subject/hlanfxtx.html