swift3.0变化
来源:互联网 发布:九章算法 视频下载 编辑:程序博客网 时间:2024/05/17 07:57
写在前面的废话: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) -> NSComparisonResultfunc compare(decimalNumber: NSNumber) -> NSComparisonResultfunc compare(otherObject: NSIndexPath) -> NSComparisonResultfunc compare(string: String) -> NSComparisonResultfunc 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
0 0
- swift3.0 gcd 变化
- swift3.0变化
- swift3.0 gcd 变化
- Swift3.0语法变化
- swift3.0的变化
- swift3.0语法变化
- Swift2.3 --> Swift3.0 的变化
- [iOS Swift3.0语法变化] 方便查询
- Swift3.0带来的变化汇总
- [iOS Swift3.0变化]iOS Swift3.0和2.3的区别
- Swift3.0相对于2.3语法的一些变化
- Swift3.0带来的变化汇总系列一——字符串与基本运算符中的变化
- Swift3.0带来的变化汇总系列一——字符串与基本运算符中的变化
- Swift3.0带来的变化汇总系列一——字符串与基本运算符中的变化
- Swift3.0带来的变化汇总系列一——字符串与基本运算符中的变化
- Swift3.0
- swift3.0
- Swift3.0
- HTTP协议(二):header标头说明
- 我读过的最好的epoll讲解--转自”知乎“
- View事件冲突 外部解决 模板写法
- 利用MSJExceptionHandler类查找Crash问题
- javascript语法结构 九
- swift3.0变化
- ubuntu更新包时产生的错误及解决方法
- zookeeper 访问控制acl
- 【Linux学习】epoll详解
- POJ 2349 Arctic Network
- Java:String、StringBuffer和StringBuilder的区别
- PHP生成唯一会员卡号
- HashMap分析
- android面试题