swift3.0 学习笔记(二)

1,885 阅读3分钟
SE-0031 inout修饰符调整 由原来的

func foo(inout x: T) // foo(_:), type (inout T) -> Void

调整为

(x: inout T) -> U // => (inout T) -> U

避免了因为参数名的错误,和参数的修饰符混淆 SE-0032 sequence增加first(where: )方法 个人感觉比较实用比较人性化的修改,各种容器例如Array,Dictionary都可以使用感觉会很好用,这很swift SE-0035 限制inout参数说明符的使用 inout是程序对入参做了一个影子拷贝,然后在作用域结束的时候将拷贝写出。通常情况下它作用的很好,但是当它与closure一起使用的时候存在问题。在3.0中对它进行了限制,closure中传入被inout修饰的参数必须使用@noescape修饰符否则编译器会报错。

func escape(f: () -> ()) {}
func noEscape(@noescape f: () -> ()) {}

func example(inout x: Int) {
  escape { _ = x } // error: closure cannot implicitly capture an inout parameter unless @noescape
  noEscape { _ = x } // OK, closure is @noescape
  escape {[x] in _ = x } // OK, immutable capture
}

struct Foo {
  mutating func example() {
    escape { _ = self } // error: closure cannot implicitly capture a mutating self parameter
    noEscape { _ = self } // OK
  }
}

补充说明:现在swift3.0所有的函数都是默认@noescape,如果你需要逃脱的话需要使用@escaping修饰符。 SE-0036 枚举实现内部也需要点作为case的前缀 在swift3.0之前在枚举内部的实现中使用case,每个case前面的点是可忽略的。swift3.0以后将会报错。

enum Coin {
    case heads, tails
    func printMe() {
        switch self {
        case heads: print("Heads")  // no leading dot error
        case .tails: print("Tails") // leading dot
        }

        if self == heads {          // no leading dot error

            print("This is a head")
        }

        if self == .tails {         // leading dot
            print("This is a tail")
        }
    }

    init() {
        let cointoss = arc4random_uniform(2) == 0
        self = cointoss ? .heads : tails // mix and match leading dots error

    }
}

(未完待续)

SE-0031 inout修饰符调整 由原来的

func foo(inout x: T) // foo(_:), type (inout T) -> Void

调整为

(x: inout T) -> U // => (inout T) -> U

避免了因为参数名的错误,和参数的修饰符混淆 SE-0032 sequence增加first(where: )方法 个人感觉比较实用比较人性化的修改,各种容器例如Array,Dictionary都可以使用感觉会很好用,这很swift SE-0035 限制inout参数说明符的使用 inout是程序对入参做了一个影子拷贝,然后在作用域结束的时候将拷贝写出。通常情况下它作用的很好,但是当它与closure一起使用的时候存在问题。在3.0中对它进行了限制,closure中传入被inout修饰的参数必须使用@noescape修饰符否则编译器会报错。

func escape(f: () -> ()) {}
func noEscape(@noescape f: () -> ()) {}

func example(inout x: Int) {
  escape { _ = x } // error: closure cannot implicitly capture an inout parameter unless @noescape
  noEscape { _ = x } // OK, closure is @noescape
  escape {[x] in _ = x } // OK, immutable capture
}

struct Foo {
  mutating func example() {
    escape { _ = self } // error: closure cannot implicitly capture a mutating self parameter
    noEscape { _ = self } // OK
  }
}

补充说明:现在swift3.0所有的函数都是默认@noescape,如果你需要逃脱的话需要使用@escaping修饰符。 SE-0036 枚举实现内部也需要点作为case的前缀 在swift3.0之前在枚举内部的实现中使用case,每个case前面的点是可忽略的。swift3.0以后将会报错。

enum Coin {
    case heads, tails
    func printMe() {
        switch self {
        case heads: print("Heads")  // no leading dot error
        case .tails: print("Tails") // leading dot
        }

        if self == heads {          // no leading dot error

            print("This is a head")
        }

        if self == .tails {         // leading dot
            print("This is a tail")
        }
    }

    init() {
        let cointoss = arc4random_uniform(2) == 0
        self = cointoss ? .heads : tails // mix and match leading dots error

    }
}

(未完待续)