前言 : Swift3.0的Swift的第3個主要版本,目標是安全,快速和有表現力,也是第一個有開源社區參與開發的Swift版本。由于語法和API改動比較多,Xcode 8.0 Beta提供了migrate遷移工具。這樣自有的代碼升級Swift3.0就比較方便了,但是,關鍵是要等第三方開源庫升級到Swift3.0啊。
那就一起來看看Swift3.0都有哪些改變吧
大家都知道Swift誕生在Objective-C已經發展的相當成熟的情況下,為了保證ObjC開發人員順利過渡到Swift,也因為Swift處于初級階段,很多類庫和方法命名都盡量和ObjC保持一致,在使用Swift開發iOS應用中處處可以看到ObjC的影子。但是作為一門Modern語言Swift還是做出了改變,從中可以看出日后Swift將徹底擺脫ObjC的影子。這其中包括重新導入Foundation消除類型前綴、方法名去重、函數和方法去C風格等等。
命名的改變
去掉C語言的風格
其它變化
獲取屏幕的bounds
在2.3中為:UIScreen.mainScreen().bounds
在3.0中變為:UIScreen.main.bounds
獲取屏幕寬度
2.3 : UIScreen.mainScreen().bounds.width
3.0 : UIScreen.main.bounds.width
獲取顏色 后面直接跟顏色的單詞即可
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提供了遷移工具。