参考

作者主页 swift官网 evolution功能演进

待解决

Escaping Closures

解释: If a closure is passed as an argument to a function and it is invoked after the function returns, the closure is escaping.

链接

mutating

nonmutating

We define the capture list by pairing the weak or unowned keywords with a reference to a class instance.

语法

extension

使用extension可以给已有的类型加入方法,属性等,还可以让已有的类型实现某个接口。 但不能override已经存在的方法。

class 和 struct 的差异

  • 类型 : struct is value type, class is reference type
  • 继承 : stuct 不能继承
  • deinitializers : struct 不能有。

在struct中 默认方法中,不能改变属性,需要在方法中改变属性时,在方法前,加mutating

Properties

  • Stored Properties
  • Computed Properties 可以理解为一个方法伪装成了一个属性, 在访问到的时候被计算. 通常使用在 为同一个数据提供不同的视图。 比如, 米,千米,

Properti observer

当properties被设置的时候调用,可以加在非懒加载的 stored properties前。

failable initializer

在init中,有可能会不满足条件。则返回 Optional ,采用的语法方式 init?(。。。。)

closure

Closures give us the ability to truly separate the data portions of our code from the user interface and business logic portions. This gives us the ability to create reusable classes that focus solely on retrieving the data. This is especially good for developing types and frameworks that are designed to retrieve data from external services, such as web services, databases, or files.

例子

可以使用和自己的定义在一个context的变量

func temperatures(calculate: (Int) -> Void) {
    var tempArray = [72,74,76,68,70,72,66]

    tempArray.map(calculate)
}

func testfunction(){
    var total = 0
    var count = 0
    let addTemps = {
        (num:Int) -> Void in
        total += num
        count += 1
    }

    temperatures(calculate: addTemps)
    print("Total : \(total)")
    print("Count : \(count)")
    print("Ave : \(total/count)")
}

testfunction()