前言 : 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提供了遷移工具。