swift3语言新特性

一. 来自swift 2.2的更新

1 弃用++和--

2 弃用C风格的for循环

// 反向循环
for i in (0 ... 10).reversed() {
    print(i)
}

for i in (0 ..< 10).reversed() {
    print(i)
}

// 步长可以是正数、负数、小数(to: 不包括20; through: 包括20)
for i in stride(from: 10, to: 20, by: 2) {
    print(i)
}
for i in stride(from: 30, to: 20, by: -2) {
    print(i)
}
for i in stride(from: 10, to: 20, by: 0.5) {
    print(i)
}

for i in stride(from: 10, through: 20, by: 2) {
    print(i)
}

3 元组的变化----支持相同类型元组之间的比较

    元组比较:先比较第一个维度,再比较第二个维度

let score1 = (chinese: 90, math: 95)
let score2 = (chinese: 90, math: 100)
let score3 = (chinese: 100, math: 90)
score1 < score2
score2 < score3

4 #selector

btn.addTarget(self, action: #selector(click), for: .touchUpInside)

二. swift3的主要变化

1 API命名原则

    (1) 第一个参数写法与之后参数的写法一致(之前第一个参数是没有外部参数名的)

    (2) 大量使用介词来表示参数的意义

    (3) 去除掉多余的文字  

var arr = [1, 2, 3, 4]
arr.index(of: 2)
arr.index(before: 4)
arr.index(after: 3)
arr.remove(at: 3)
arr.insert(5, at: 2)
arr

var string = "Hello"
string.stringByAppendingString("ABC")   //之前写法  
string.append("ABC")

2 使用swift3创建自己的函数

// swift2中函数定义
func welcomeToName(name: String, with message: String) -> String {
    return message + "  " + name
}

welcomeToName(name: "zijie", with: "Hello")

swift3定义函数注意

(1) 可以只定义内部参数名,不用定义外部参数名

func welcome(name: String, message: String) -> String {
    return message + "  " + name
}
welcome(name: "zijie", message: "Hello")

(2)使用介词来充当外部参数名

func welcome(to name: String, message: String) -> String {
    return message + "  " + name
}
welcome(to: "zijie", message: "Hello")

(3)使用‘_’来充当外部参数名

func add(_ a: Int, _ b: Int) -> Int {
    return a + b
}
add(4, 5)

3 表意更清晰的函数名

func changeScore(scores: [Int], change: (Int) -> Int) {
}
func changeScore(a: int) -> (Int)-> Int {
}

上述方法中Int类型的括号不可省略

func addStudent(name: String, at index: Int) {
}
func addStudent(name: String, before student: String) {
}
func addStudent(name: String, after student: String) {
}

let f1 = addStudent(name:at:)
let f2 = addStudent(name:before:)
let f3 = addStudent(name:after:)

对于方法名字相同的函数,函数赋值时要在括号中增加参数,这样可以更明确的说明定义的是哪个函数。

4 动词和名词

  尾缀-ed表示过去时,尾缀-ing表示名词:有返回值,不影响原对象。

  没有这些尾缀,则为动作,直接作用于原对象

var string = "Hello"            //"Hello"
string.appending(" zijie")      //"Hello  zijie"
string                          //"Hello"
string.append(" zijie")         //"Hello  zijie"
string                          //"Hello  zijie"
var scores = [4, 3, 6, 7, 2, 8]     //[4, 3, 6, 7, 2, 8]
scores.sorted()                     //[2, 3, 4, 6, 7, 8]
scores                              //[4, 3, 6, 7, 2, 8]
scores.sort()                       //[2, 3, 4, 6, 7, 8]
scores                              //[2, 3, 4, 6, 7, 8]

5 枚举类型的变化-----首字母小写

enum ErrorState: String {
    case netError
    case errorData
}

6 where的变化

 以下语句为swift2语句,其中case 定义模式,where为约束条件‘x > 2 && x < 5’。在swift3中,where化简为一个‘,’

//swift2语法
let vector = (4, 0) if case (let x, 0) = vector where x > 2 && x < 5 { print("success") }
//swift3语法
let vector = (4, 0) if case (let x, 0) = vector , x > 2 && x < 5 { print("success") }
// swift2代码风格
func doMath(a: Int?, b: Int?, c: Int?) {
    guard let a = a where a > 0,
            let b = b where b < 0,
            let c = c where c % 2 == 0 else {
            return
    }
}

// swift3苹果建议风格-------第一行解包,第二行bool值判断。每一个解包都得加上let关键词 
func doMath(a: Int?, b: Int?, c: Int?) {
    guard let a = a, let b = b, let c = c,
        a > 0, b < 0, c % 2 == 0 else {
            return
    }
}

 7 摒弃C风格函数

posted @ 2016-11-15 19:45  紫洁  阅读(1223)  评论(0编辑  收藏  举报