我指的是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"];
数组
原文链接:https://www.f2er.com/swift/321480.html创建不可变数组
第一种方式:
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"