獲取屏幕的bounds
在2.3中: UIScreen.mainScreen().bounds
在3.0中: UIScreen.main.bounds
獲取顏色
在2.3中: UIColor.orangeColor()
在3.0中: UIColor.orange
KVC字典轉(zhuǎn)模型方法的改變
在2.3中: setValuesForKeysWithDictionary(dict)
在3.0中: setValuesForKeys(dict)
在2.3中的任意對象 AnyObjcet 在3.0中變?yōu)?Any
注冊cell方法的改變 省略了一個(gè)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)
設(shè)置字體
在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
按鈕設(shè)置文字 和監(jiān)聽按鈕點(diǎn)擊方法的改變
在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: {}
生產(chǎn)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) 變?yōu)镮ndexPath
在使用的時(shí)候要轉(zhuǎn)為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]
CGPointZero在3.0中改為 CGPointzero
圖片的內(nèi)容模式 .Center 在3.0 中改為 .center
hidden屬性在3.0 中改為 isHidden
selected屬性在3.0中改為 isSelected
寫入文件writeToFile 在3.0中改為write
聲明私有的成員變量和方法的關(guān)鍵字 從Private 改為 fileprivate
分頁屬性的從pagingEnabled 變?yōu)?isPagingEnabled
設(shè)置URL 從 NSURL(string: string) 變?yōu)?URL(string: string)
設(shè)置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
}
獲取一個(gè)控件的最大Y或X值的方法
2.3 : CGRectGetMaxY((imageView?.frame)!)
3.0 : (imageView?.frame)!.maxY
獲取UIApplication方法 //改變狀態(tài)欄的顏色
2.3 : UIApplication.sharedApplication().statusBarStyle = .LightContent
3.0 : UIApplication.shared.statusBarStyle = .lightContent
設(shè)置形變
2.3 : loginView.transform = CGAffineTransformMakeScale(0, 0)
3.0 : loginView.transform = CGAffineTransform(scaleX: 0, y: 0)
總結(jié)
Swift的每次變化由于對之前的版本乃至上一個(gè)版本都不兼容造成每次Swift的升級都顯得比較虐心,但是事實(shí)上這也是Swift的重大進(jìn)步。記得之前曾有傳聞?wù)fSwift3.0的語法和API都會穩(wěn)定并且向上兼容,但是不久這個(gè)消息就破滅了,WWDC上官方也再次證實(shí)這個(gè)希望可能要到4.0才能實(shí)現(xiàn)。但是試想一下:Apple在很短的時(shí)間內(nèi)就固話API對于Swift的發(fā)展真的是好事嗎?畢竟新特性的加入、更好的語法優(yōu)化才能讓Swift越來越好!總的來說,如果應(yīng)用要升級到Swift3.0可能要做不同程度的修改,但是這種改動僅僅是語法和SDK的變動并不會消耗太多的工作量,更何況Apple提供了遷移工具。