总结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 的异常处理机制。如下代码:
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()
}
好了,想到的就这些了,其他改变的比较常见,就没有记录了。
以下列举的是我在项目中遇到的需要修改的,基本常见的问题就没有罗列了。
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()
}
好了,想到的就这些了,其他改变的比较常见,就没有记录了。
相关文章推荐
- 《从零开始学Swift》学习笔记(Day 13)——数据类型之整型和浮点型
- Swift第三课 分支语句 if else for forin switch while do-while
- Swift 2.0学习笔记(Day 13)——数据类型之整型和浮点型
- swift2.0 Cannot assign a value of type '[CFString]' to a value of type '[String]'
- Swift学习笔记(二十八)——Switch-Case的高级用法
- Swift学习笔记(二十七)——条件语句和Switch-case语句
- Swift学习 第一课,变量与常量
- Swift 2.0 : 'enumerate' is unavailable: call the 'enumerate()' method on the sequence
- Swift基础语法详解(一)
- Swift之贪婪的UIButton
- Swift中的NSKeyedArchiver使用
- Swift中的单例的实现方式
- Swift_基础部分
- Swift 2:可以使用 @NSManage 标记自动生成方法
- Swift 2.0学习笔记(Day 1)——我的第一行Swift代码
- Swift UIScrollView简单介绍
- Swift中自定义SubString
- 【iOS】Swift中扩展extension与协议protocol
- Swift Tutorial(swift 教程) 5:Making it Beautiful
- 关东升的《iOS实战:图形图像、动画和多媒体卷(Swift版)》上市了