Session402-What new in Swift

2017-07-02  本文已影响0人  雷侯塞利

Private 权限控制

Swift3中,如果将主体函数的变量定义为private,则其extension无法读取此变量,必须将其改为filePrivate才可以,但是权限又被扩大了。在Swift4中,private变量也可以被其extension读取。

Swift3.0时Private权限 Swift4.0时Private权限

类和接口的组合语法支持

Swift4.0中泛型支持类和接口的组合

类和接口的组合

编译相关

加快了编译速度

减少Swift暴露给OC的接口

在之前的Swift 版本中,所有继承自NSObject的类的属性和方法都会被默认加上@objc标识,以便将其暴露给Objective-C,编译器会为这些方法和属性生成可供OC调用的代码,但是实际情况可能只有很少量的几个方法需要暴露给OC,这就造成很大的空间浪费。因此在Swift 4中,编译器大部分情况下不会自动添加@objc标识,如果要暴露给OC,需要手动添加。

Before Swift 4, the compiler made some Swift declarations automatically available to Objective-C. For example, if one subclassed from NSObject, the compiler created Objective-C entry points for all methods in such classes. The mechanism is called @objc inference.

In Swift 4, such automatic @objc inference is deprecated because it is costly to generate all those Objective-C entry points. When "Swift 3 @objc Inference" setting is set to "On", it allows the old code to work. However, it will show deprecation warnings that need to be addressed. It is recommended to "fix" these warnings and switch the setting to "Off", which is the default for new Swift projects.


可以通过以上设置开启/关闭Swift3的 @objc推断

String相关

编译器自动给每行前面添加换行符号 屏幕快照 2017-07-02 上午11.36.07.png 屏幕快照 2017-07-02 上午11.37.53.png 屏幕快照 2017-07-02 上午11.39.04.png

Associated type constraints 关联类型约束

protocol SomeProtocol where Self: UICollectionViewCell {
}
extension Sequence where Element: Numeric {
  var sum: Element {
      var result: Element = 0
      for element in self {
          result += element
      }
      return result
  }
}
[1,2,3,4].sum

强制要求对内存变量的访问具有互斥性

例如,在遍历一个数组的过程中如果同时在循环内对数组进行数据操作,会在编译时报错。

屏幕快照 2017-07-02 上午11.43.42.png 屏幕快照 2017-07-02 上午11.43.42.png
上一篇 下一篇

猜你喜欢

热点阅读