1.如何使用定义我们常用的OC类,比如NSString,NSDictionary,NSArray
1.1定义字符串
1.1.1定义不可变字符串
oc:
NSString * str = @"hello world";
swift:
let str = "hello world"
1.1.2定义可变字符串
oc:
NSMutableString *mustr = [[NSMutableString alloc]init];
[mustr appendString:@"hello"];
[mustr appendString:@"world"];
swift:
var str = ""
str += "hello"
str += "world"
1.2定义字典
1.2.1定义非空字典
oc:
NSDictionary *dic=@{@"key":@"value"};
swift:
let dic = ["value":"key"]
1.2.2定义空字典
oc:
NSDictionary *dic=@{};
swift:
let dic = Dictionary<String,String>()
必须制定字典中尖括号里面的类型,因为swift是强类型语言,而OC是弱类型语言其中第一个String为key对应的类型,第二个String为value对应的类型。
1.3定义字典
1.3.1定义非空数组
ocNSArray *arr =@[@"11",@"22"];
swift:let arr = ["value":"key"]
1.2.2定义空数组
oc:NSDictionary *dic=@[];
swift:
let arr = Array<String>()或let arr = [String]()
数组中的String是所装载的类型