前言 : Swift3.0的Swift的第3个主要版本,目标是安全,快速和有表现力,也是第一个有开源社区参与开发的Swift版本。因为语法和API改动比较多,Xcode 8.0 Beta提供了migrate迁移工具。这样自有的代码升级Swift3.0就比较方便了,可是,关键是要等第三方开源库升级到Swift3.0啊。php
那就一块儿来看看Swift3.0都有哪些改变吧css
你们都知道Swift诞生在Objective-C已经发展的至关成熟的状况下,为了保证ObjC开发人员顺利过渡到Swift,也由于Swift处于初级阶段,不少类库和方法命名都尽可能和ObjC保持一致,在使用Swift开发iOS应用中到处能够看到ObjC的影子。可是做为一门Modern语言Swift仍是作出了改变,从中能够看出往后Swift将完全摆脱ObjC的影子。这其中包括从新导入Foundation消除类型前缀、方法名去重、函数和方法去C风格等等。安全
命名的改变ruby
去掉C语言的风格ide
其它变化函数
获取屏幕的bounds工具
在2.3中为:UIScreen.mainScreen().bounds 在3.0中变为:UIScreen.main.bounds
获取屏幕宽度布局
2.3 : UIScreen.mainScreen().bounds.width 3.0 : UIScreen.main.bounds.width
获取颜色 后面直接跟颜色的单词便可post
2.3: UIColor.orangeColor() 3.0: UIColor.orange
KVC字典转模型方法的改变字体
2.3 : setValuesForKeysWithDictionary(dict) 3.0 : setValuesForKeys(dict)
在2.3中的任意对象 AnyObjcet 在3.0中变为 Any
注册cell方法的改变 省略了一个Nib单词 同理注册class的cell 省略class单词
2.3 : tableView.registerNib(UINib(nibName: "XTLiveTableViewCell", bundle: nil), forCellReuseIdentifier: ID) 3.0 : tableView.register(UINib(nibName: "XTLiveTableViewCell", bundle: nil), forCellReuseIdentifier: ID)
设置字体
2.3 : UIFont.systemFontOfSize(17)
3.0 : UIFont.systemFont(ofSize: 17)
获取文字宽度
2.3 : (title! asNSString) .sizeWithAttributes(nameAttr).width 3.0 : (title! asNSString) .size(attributes: nameAttr).width
按钮设置文字 和监听按钮点击方法的改变
2.3 : btn.setTitle(vc.title, forState: .Normal) btn.setTitleColor(UIColor.grayColor(), forState: .Normal) btn.setTitleColor(UIColor.redColor(), forState: .Selected) btn.addTarget(self, action: #selector(XTBaseViewController.btnClick(_:)), forControlEvents: .TouchUpInside) 3.0 : btn.setTitle(vc.title, for: UIControlState()) btn.setTitleColor(UIColor.gray, for: UIControlState()) btn.setTitleColor(UIColor.red, for: .selected) btn.addTarget(self, action: #selector(XTBaseViewController.btnClick(_:)), for: .touchUpInside)
动画方法的改变
2.3 : UIView.animateWithDuration(0.25, animations:{} 3.0 : UIView.animate(withDuration: 0.25, animations: {}
生成cell
2.3 : let cell = collectionView.dequeueReusableCellWithReuseIdentifier(ID, forIndexPath: indexPath) 3.0 : let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ID, for: indexPath)
在3.0中cell的indexPath的类型(原来为NSIndexPath) 变为IndexPath
在使用的时候要转为NSIndexPath再去使用
2.3 : func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {} let vc = childViewControllers[indexPath.row] 3.0 : func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {} let vc = childViewControllers[(indexPath asNSIndexPath).row]
获取mainBundle方法的改变
2.3 : NSBundle.mainBundle()
3.0 : Bundle.main
CGPointZero在3.0中改成 CGPointzero
图片的内容模式 .Center 在3.0 中改成 .center
hidden属性在3.0 中改成 isHidden
selected属性在3.0中改成 isSelected
写入文件writeToFile 在3.0中改成write
声明私有的成员变量和方法的关键字 从Private 改成 fileprivate
分页属性的从pagingEnabled 变为 isPagingEnabled
设置URL 从 NSURL(string: string) 变为 URL(string: string)
设置UICollectionView的布局方法
2.3 : overridefunc prepareLayout() { super.prepareLayout() } 3.0 : overridefunc prepare() { super.prepare() }
dismiss掉控制器
2.3 : dismissViewControllerAnimated(false, completion: nil) 3.0 : dismiss(animated: false, completion: nil)
通知方法的改变
2.3 : NSNotificationCenter.defaultCenter().postNotificationName("loginClick", object: nil) 3.0 : NotificationCenter.default.post(name: Notification.Name(rawValue: "loginClick"), object: nil)
定义枚举 枚举值只能用小写
2.3 : enum RequestType { case GET case POST } 3.0 : enum RequestType { case get case post }
获取一个控件的最大Y或X值的方法
2.3 : CGRectGetMaxY((imageView?.frame)!) 3.0 : (imageView?.frame)!.maxY
获取UIApplication方法
2.3 : //改变状态栏的颜色 UIApplication.sharedApplication().statusBarStyle = .LightContent 3.0 : UIApplication.shared.statusBarStyle = .lightContent
设置形变
2.3 : loginView.transform = CGAffineTransformMakeScale(0, 0) 3.0 : loginView.transform = CGAffineTransform(scaleX: 0, y: 0)
总结
Swift的每次变化因为对以前的版本乃至上一个版本都不兼容形成每次Swift的升级都显得比较虐心,可是事实上这也是Swift的重大进步。记得以前曾有传闻说Swift3.0的语法和API都会稳定而且向上兼容,可是不久这个消息就破灭了,WWDC上官方也再次证明这个但愿可能要到4.0才能实现。可是试想一下:Apple在很短的时间内就固话API对于Swift的发展真的是好事吗?毕竟新特性的加入、更好的语法优化才能让Swift愈来愈好!总的来讲,若是应用要升级到Swift3.0可能要作不一样程度的修改,可是这种改动仅仅是语法和SDK的变更并不会消耗太多的工做量,更况且Apple提供了迁移工具。