加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

在Swift中的ios – 不可变/可变集合

发布时间:2020-12-14 06:12:38 所属栏目:百科 来源:网络整理
导读:我指的是Apple的Swift编程指南,用于了解Swift语言中Mutable / immutable对象(Array,Dictionary,Sets,Data)的创建。但我不知道如何在Swift中创建一个不可变的集合。 我想在Objective-C中查看Swift中的以下内容 不可变数组 NSArray *imArray = [[NSArray a
我指的是Apple的Swift编程指南,用于了解Swift语言中Mutable / immutable对象(Array,Dictionary,Sets,Data)的创建。但我不知道如何在Swift中创建一个不可变的集合。

我想在Objective-C中查看Swift中的以下内容

不可变数组

NSArray *imArray = [[NSArray alloc]initWithObjects:@"First",@"Second",@"Third",nil];

可变阵列

NSMutableArray *mArray = [[NSMutableArray alloc]initWithObjects:@"First",nil];
[mArray addObject:@"Fourth"];

不变字典

NSDictionary *imDictionary = [[NSDictionary alloc] initWithObjectsAndKeys:@"Value1",@"Key1",@"Value2",@"Key2",nil];

可变字典

NSMutableDictionary *mDictionary = [[NSMutableDictionary alloc]initWithObjectsAndKeys:@"Value1",nil];
[mDictionary setObject:@"Value3" forKey:@"Key3"];
数组

创建不可变数组

第一种方式:

let array = NSArray(array: ["First","Second","Third"])

第二种方式:

let array = ["First","Third"]

创建可变数组

var array = ["First","Third"]

将对象附加到数组

array.append("Forth")

词典

创建不可变字典

let dictionary = ["Item 1": "description","Item 2": "description"]

创建可变字典

var dictionary = ["Item 1": "description","Item 2": "description"]

将新对附加到字典

dictionary["Item 3"] = "description"

More information on Apple Developer

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读