Swift与OC转换
2015-09-16 20:30
453 查看
1、获取对象类型
OC:
NSDate* date = [NSDatedate];
NSLog(@"%@",NSStringFromClass([date class]));
Swift:
letdate =NSDate()
letname = date.dynamicType
共有:
let name:AnyClass! = object_getClass(date)
2、函数入参中对象转化
OC转Swift,对象变为可选类型
Swift转OC,不用改变
3、枚举
1)Swift按位操作使用OptionSetType
[/code]
Now we can use set-based semantics with
}
2)OC调用Swift,只需增加前缀
[/code]
Shamelessly taken from the Swift Blog
In Objective-C this would look like
3)Swift调用OC直接使用rawValue
OC:
NSDate* date = [NSDatedate];
NSLog(@"%@",NSStringFromClass([date class]));
Swift:
letdate =NSDate()
letname = date.dynamicType
共有:
let name:AnyClass! = object_getClass(date)
2、函数入参中对象转化
OC转Swift,对象变为可选类型
Swift转OC,不用改变
3、枚举
1)Swift按位操作使用OptionSetType
struct MyOptions : OptionSetType { let rawValue: Int static let None = MyOptions(rawValue: 0) static let FirstOption = MyOptions(rawValue: 1 << 0) static let SecondOption = MyOptions(rawValue: 1 << 1) static let ThirdOption = MyOptions(rawValue: 1 << 2) }
[/code]
Now we can use set-based semantics with
MyOptions:
let singleOption = MyOptions.FirstOption let multipleOptions: MyOptions = [.FirstOption, .SecondOption] if multipleOptions.contains(.SecondOption) { print("multipleOptions has SecondOption") } let allOptions = MyOptions(rawValue: 7) if allOptions.contains(.ThirdOption) { print("allOptions has ThirdOption")
}
2)OC调用Swift,只需增加前缀
@objc
@objc enum Bear: Int { case Black, Grizzly, Polar }
[/code]
Shamelessly taken from the Swift Blog
In Objective-C this would look like
Bear type = BearBlack; switch (type) { case BearBlack: case BearGrizzly: case BearPolar: [self runLikeHell]; }
3)Swift调用OC直接使用rawValue
相关文章推荐
- 初步swift语言学习笔记6(ARC-自己主动引用计数,内存管理)
- Swift 第五课 闭包
- Swift学习笔记(三十四)——函数类型
- Swift学习笔记(三十三)——常量参数,变量参数和inout参数
- Swift学习笔记(三十二)——可变参数
- 语法参考系列——(一)如何读懂Swift的语言参考
- Swift学习笔记(三十一)——参数的默认值
- Swift学习笔记(三十)——函数的内部参数名和外部参数名
- Swift学习笔记(二十九)——函数的使用1
- 总结swi1.2适配swift2.0遇到的改变
- 《从零开始学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