写在前面的废话:swift3.0改变了很多东西,以前的项目升级到xcode8后,报了好多错误,实在是无语。不过当了解了swift3.0新的特性后,还是抱有很大的期待。
1.函数参数传入参数为不可变类型
原因:var和inout在函参中常常混淆,var常常使值类型引用混淆
第一种写法是非法的,第二种是改进的写法
func foo(i: Int) {
i += 1 // 非法
}
func foo(i: Int) {
var i = i
}
2.函数/方法的改变
1.去除冗余的名称
去除冗余名称的方法步骤:
a.和类型相匹配的第一个词
func colorWithAlphaComponent(_: CGFloat) -> NSColor//去除之前
func WithAlphaComponent(_: CGFloat) -> NSColor//去除之后
b.删除额外的条件词'by'
func stringByApplyingTransform(_: NSString, reverse: Bool) -> NSString?//去除之前
func applyingTransform(_: NSString, reverse: Bool) -> String?//去除之后
c.删除任何匹配类型的尾巴
func documentForURL(_ url: NSURL) -> NSDocument? // 删除之前
class func darkGrayColor() -> UIColor // 删除之前
func documentFor(_ url: NSURL) -> NSDocument? // 删除之后
class func darkGray() -> UIColor //删除之后
d.删除动词后的类型名称
func dismissViewControllerAnimated(flag: Bool, completion: (() -> Void)? = nil)//删除之前
func dismissAnimated(flag: Bool, completion: (() -> Void)? = nil)// 删除之后
注意:"get", "set", "with", "for", or "using"有这几个词的方法一般都没有变化
2.添加默认参数
添加条件:
a.最后一个参数可以为nil
rootViewController.presentViewController(alert, animated: true, completion: nil)//添加之前,最后的completion可以传入nil
rootViewController.present(alert, animated: true)//添加之后
b.类型设置如果有默认值
UIView.animateWithDuration(
0.2, delay: 0.0, options: [], animations: { self.logo.alpha = 0.0 }) {
_ in self.logo.hidden = true
}//添加之前,options有默认值设置
UIView.animateWithDuration(
0.2, delay: 0.0, animations: { self.logo.alpha = 0.0 }) { _ in self.logo.hidden = true }//添加之后
3.给方法/函数第一个参数添加标签
func enumerateObjectsWith(_: NSEnumerationOptions = [], using: (AnyObject, UnsafeMutablePointer) -> Void)//之前版本
func enumerateObjects(options _: NSEnumerationOptions = [], using: (AnyObject, UnsafeMutablePointer) -> Void)//swift3版本
4.bool变量添加’is’
var isEmpty: Bool
5.比较方法的统一性
例如:
func compare(other: NSDate) -> NSComparisonResult
func compare(decimalNumber: NSNumber) -> NSComparisonResult
func compare(otherObject: NSIndexPath) -> NSComparisonResult
func compare(string: String) -> NSComparisonResult
func compare(otherNumber: NSNumber) -> NSComparisonResult
3.应用API指南标准库
API变化一般可以总结为一下几点:(-/+表示旧版本和新版本)
a.带'Type'后缀的协议(swift3弃用带Protocol后缀的协议,当然还在修改中)
-public protocol BooleanType { ... }
+public protocol Boolean { ... }
-public protocol IntegerType : ... { ... }
+public protocol Integer : ... { ... }
...
b.所有的API中generator相关重命名为iserator
-public protocol GeneratorType { ... }
+public protocol IteratorProtocol { ... }
-func generate() -> Generator
+func makeIterator() -> Iterator
...
c.Bit类型被移除,使用Int替代
d.不安全的泛型参数从Memory改为Pointee
-public var memory: Memory { get set }
+public var pointee: Pointee { get set }
e.无参数的初始化方法被移除,建议使用nil代替
-public init()//使用'nil'代替
f.PermutationGenerator被移除
g.MutableSliceable被移除,使用Collection where SubSequence : MutableCollection代替
h.sort() 方法变为 sorted(), sortInPlace() 方法变为sort()
-public func sort() -> [Generator.Element]
+public func sorted() -> [Iterator.Element]
i.reverse() 方法变为 reversed()
j.enumerate() 方法变为 enumerated()
k.SequenceType.minElement() 方法变为 .min(), .maxElement() 方法变为 .max().
l.一些方法变成了属性,反之亦然
-public func unsafeUnwrap<T>(nonEmpty: T?) -> T
+public var unsafelyUnwrapped: Wrapped { get }
...
-public var lowercaseString: String { get }
+public func lowercased()
m.枚举变为小写开头
-public static var NaN: Float
+public static var nan: Float
-case SignalingNaN
+case signalingNaN
当然还有很多变化,就不一一说明,需要了解的可以去这里看看
swift3.0API的变化
4.类型变化
Objc中的id类型对应swift中的Any