OC与swift对比一之常用数据类型

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是所装载的类型

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容