您的位置:首页 > 移动开发 > Swift

总结swi1.2适配swift2.0遇到的改变

2015-09-16 14:11 465 查看
swift1.2适配swift2.0

以下列举的是我在项目中遇到的需要修改的,基本常见的问题就没有罗列了。

1.find函数变成了为indexOf

2.sort变成了sortInPlace

3.sorted变成了sort

4.enumerate(self.tableView.visibleCells)修改为self.tableView.visibleCells.enumerate()

5.Printable 变成了CustomStringConvertible

6.DebugPrintable 变成了CustomDebugStringConvertible

7.结构体:GeneratorOf 变成了 AnyGenerator

8.translatesAutoresizingMaskToConstraints = false代替了setTranslatesAutoresizingMaskToConstrains(false)。

9.UITableView的dequeueReusableCellWithIdentifier方法返回值为UITableViewCell?所以不用as UITableViewCell

10.tableView.indexPathsForVisibleRows() 返回值为[AnyObject]修改为tableView.indexPathsForVisibleRows 返回值为 [NSIndexPath]

11.NSDataBase64EncodingOptions.allZeros 替换为NSDataBase64EncodingOptions()

12.error 全数替换成了 throw,使用throw catch 的异常处理机制。如下代码:

使用do try cacth
//mqy
do{
let player = try AVAudioPlayer(contentsOfURL: url!)
player.volume = 0.005
player.prepareToPlay()
player.play()
delay(1, task: { () -> () in
player.stop()
})

}catch let error as NSError{
print("error:\(error)")
}

//       var player  = AVAudioPlayer( contentsOfURL: url!, error: nil)
//        player.volume = 0.005
//        player.prepareToPlay()
//        player.play()
//        delay(1, task: { () -> () in
//            player.stop()
//        })


13.tableview的init方法

// override init(frame: CGRect) {

// super.init(frame: frame)

// }

改变为如下:

override init(frame: CGRect, style: UITableViewStyle) {

super.init(frame: frame, style: UITableViewStyle.Plain)

self.setUp()

}

14.指示startUserActivity()方法只在iOS8.0+,OSX10.10+以及以其他平台的全版本可用。

@available(iOS 8.0, OSX 10.10, *)

func startUserActivity() -> NSUserActivity {

}

15. button

let back = UIButton.buttonWithType(UIButtonType.Custom) as! UIButton

改变为

let back = UIButton(type: UIButtonType.Custom)

16.enumerate

for (idx,data) in enumerate(self.dataArray){}

改变为

for (idx,data) in self.dataArray.enumerate(){}

17. supportedInterfaceOrientations 函数返回值类型改变

override func supportedInterfaceOrientations() -> Int {

return Int(UIInterfaceOrientationMask.All.rawValue)

}

返回值int修改为UIInterfaceOrientationMask:

override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {

return UIInterfaceOrientationMask.All

}

18.

delay(2, { callback(type: RealtimeService.CallbackType.Connected, room: room, data: "") })

改变为需要task:

delay(2, task: { callback(type: RealtimeService.CallbackType.Connected, room: room, data: "") })

19.

UIColorFromRGBA(0xFFFFFF, 0.2)

改变为:

UIColorFromRGBA(0x000000, alpha: 0.3)

20.使用到|语句的都改变为如下

UIView.transitionWithView(self.likeButton, duration: 1, options: UIViewAnimationOptions.TransitionFlipFromLeft | UIViewAnimationOptions.AllowUserInteraction, animations:

改变为:

UIView.transitionWithView(self.likeButton, duration: 1, options: [UIViewAnimationOptions.TransitionFlipFromLeft , UIViewAnimationOptions.AllowUserInteraction], animations: { () -> Void

21. filter

let numbers = [1, 5, 6, 10, 16, 42, 45]

find(filter(map(numbers, { $0 * 2}), { $0 % 3 == 0 }), 90)

改变为如下

// Swift 2

numbers.map { $0 * 2 }.filter { $0 % 3 == 0 }.indexOf(90)

22.使用@objc 必须是继承自NSObject类型

23.

override func observeValueForKeyPath(keyPath: String, ofObject object: AnyObject, change: [NSObject : AnyObject], context: UnsafeMutablePointer<Void>) {

<#code#>

}

改变为:

override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {

MBProgressHUD.hideHUDForView(self.view, animated: true)

self.loadProductArray()

self.collectionView.reloadData()

}

好了,想到的就这些了,其他改变的比较常见,就没有记录了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: