swift5(4)集合类型
2020-02-26 本文已影响0人
C_G__
感谢作者
https://www.jianshu.com/p/389e9d7b022e
// swift5 (4) - 集合类型
// Array是有序的值的集合。
// Set是唯一值的无序集合。
// Dictionary是键值关联的无序集合。
// Array
//方式一
let intArray1 = Array<Int>();
print("intArray1中有\(intArray1.count)个元素");
//方式二
let intArray2 = [Int]();
print("intArray2中有\(intArray2.count)个元素");
let defaultValueArray = Array.init(repeating: "你好", count: 3);
print(defaultValueArray);//!< ["你好", "你好", "你好"]
// 两个数组相加
let secondValueArray = Array.init(repeating: "大佬", count: 2);
let thirdValueArray = defaultValueArray + secondValueArray;
print(thirdValueArray);//!< ["你好", "你好", "你好", "大佬", "大佬"]
// 使用字面量元素初始化数组
var literalArray = ["逗哥","刀哥"]; //!< ["逗哥", "刀哥"] 有初值,可以类型推断,不必声明数组的类型
let literalArray1 : [Int] = [6,7]; //!< [6, 7] 声明了类型
// 数组的访问和修改
// 判空 和 长度
if intArray1.isEmpty {
print("intArray1:\(intArray1)中有\(intArray1.count)个元素");
//!< intArray:[]中有0个元素
}
// 追加
var appendArray = ["飞哥","强哥"];
appendArray.append("大哥");//!< ["飞哥", "强哥", "大哥"]
let array1 = ["finally","ok"];
let array2 = ["拯救","静","\u{65}"];
appendArray += array1; //!< ["飞哥", "强哥", "finally", "ok"]
appendArray += array2; //!< ["飞哥", "强哥", "finally", "ok", "拯救", "静", "e"]
// 获取元素
let firstStr = literalArray[0];
print(firstStr); //!< 逗哥
// 修改指定下标
literalArray[0] = "加油";
print(literalArray); //!< ["加油", "刀哥"]
// replace
var totalArray = ["飞哥", "强哥", "finally", "ok", "拯救", "静", "e"];
totalArray[...3] = ["","","","❌"];
//!< ["", "", "", "❌", "拯救", "静", "e"]
totalArray[5..<7] = ["融合了"];
//!< 5..<7有两个元素 被替换成了一个 log:["", "", "", "❌", "拯救", "融合了"]
print(totalArray);
// insert
literalArray.insert("妄言", at: 1);//!< ["逗哥", "妄言", "刀哥"]
print(literalArray);
// remove
let deleteStr = literalArray.remove(at: 0)//!< ["妄言", "刀哥"]
print(literalArray,"删除了:\(deleteStr)")//!< ["妄言", "刀哥"] 删除了:逗哥
literalArray.removeLast();
literalArray.removeFirst();
// 遍历
for item in literalArray {
print(item,"", separator: " ", terminator: ""); //!< 逗哥 刀哥
}
for item in literalArray.enumerated() {
print(item.1); //!< 逗哥 刀哥
print(item.0); //!< 0 1
print(item); //!< (offset: 0, element: "逗哥") (offset: 1, element: "刀哥")
}
// Set
// 必须是可散列的类型才能存储在集合中
// 集合中存储的类型必须提供能够计算自身散列值的方法。
// 哈希值是一个Int值,对于所有相互比较相等的对象都是相同的,
// 例如a == b,则遵循a.hashValue == b.hashValue
let emptySet = Set<Int>();
let emptyCharacterSet = Set<Character>();
var favoriteGenres :Set<String> = ["可是","怎么","能够","如此"];
var favoriteGenres2 :Set = ["可是","怎么","能够","如此"];
// 集合的访问和修改
if favoriteGenres.isEmpty {
print("favoriteGenres集合中没有元素");
} else {
print("favoriteGenres集合有 \(favoriteGenres.count)个元素");
}
// insert
favoriteGenres.insert("聪明");//!< ["聪明", "能够", "可是", "怎么", "如此"]
// remove
if let removeItem = favoriteGenres.remove("聪明") {
print("\(removeItem)此项已被移除");
} else {
print("favoriteGenres集合中没有该项");
}
// contains
if favoriteGenres.contains("好的") {
print("包含");
} else {
print("不包含");
}
// 遍历
for item in favoriteGenres {
print(item,"",separator: " ", terminator: "");
//!< 能够 怎么 如此 可是
}
for item in favoriteGenres.sorted() {
print(item);
}
let set1 : Set<Int> = Set.init(arrayLiteral: 1,5,7,2);
let set2 : Set<Int> = [2,4,6,8];
// 交集
let set3 = set1.intersection(set2);
//!< 预期:[2] 实际 [2]
// 对称差集
let set4 = set1.symmetricDifference(set2);
//!< 预期:[1,5,7,4,6,8] 实际: [6, 4, 5, 7, 8, 1]
// 并集
let set5 = set1.union(set2);
//!< 预期[6, 4, 5, 7, 8, 1,2] 实际 [4, 5, 2, 8, 1, 7, 6]
// 差集
let set6 = set1.subtracting(set2);
//!< 预期[1,5,7] 实际[1, 7, 5]
let set7 : Set<Int> = [1,5,2,7];
let set8 : Set<Int> = Set.init(arrayLiteral: 1,5,7,2,8);
//! 判断相等
if set7 == set8 {
print("相等");
} else {
print("不相等");
}
//!判断子集与超集
if set7.isStrictSubset(of: set8) {//!< set是否是set1的子集
print("set是set1的子集");
}
// 或者isStrictSuperset
if set8.isStrictSubset(of: set7) {//!< set1是否是set的超集
print("set1是set的超集");
}
//!< 判断是否不相交
if set8.isDisjoint(with: set7) {//!< `true`元素不相交 否则`false`
print("set1和set不相交");
} else {
print("set1和set相交");
}
// Dictionary
// Key表示字典中键的值的类型,Value表示存储的值的类型。
//方式一
let namesOfIntegers1 :Dictionary<Int,String> = Dictionary<Int,String>.init();
let namesOfIntegers2 = Dictionary<Int,String>.init();
let namesOfIntegers3 = Dictionary<Int,String>();
//方式二
let namesOfIntegers4 = [Int:String].init(); //!< [:]
let namesOfIntegers5 = [Int:String]();
//方式三:提供类型信息
let namesOfIntegers6 : [Int:String] = [:];
let airports : [String:String] = [String:String].init(dictionaryLiteral: ("name","zhangfei"),("age","16"),("职业","将军"));
//!< ["职业": "将军", "age": "16", "name": "zhangfei"]
//键值类型都相同,swift乐行推断,故省略字典类型
let airports1 = ["职业": "将军", "age": "16", "name": "zhangfei"];
//以下类型的字典就不能类型推断,必须声明字典的键值类型
let airports2 : [String : Any] = ["职业": "将军", "age": 16, "name": "zhangfei"];
// 字典的访问和修改
if airports.isEmpty {
print("airports是空的");
} else {
print("airports:\(airports)中有\(airports.count)个元素");
//!< airports:["name": "zhangfei", "职业": "将军", "age": "16"]中有3个元素
}
//方式一.使用下标语法
var airports3 = ["job": "将军", "age": "16", "name": "zhangfei"];
airports3["sex"] = "男";
//方式二
// updateValue(_:forKey:)方法
// 若key已存在,则更改其值,返回旧值。
// 若key不存在,则返回nil,并添新值。
var airports4 = ["job": "将军", "age": "16", "name": "zhangfei"];
airports4.updateValue("男", forKey: "sex");
print(airports);
//!< ["sex": "男", "job": "将军", "age": "16", "name": "zhangfei"]
//方式一.使用下标语法
var airports5 = ["job": "将军", "age": "16", "name": "zhangfei"];
airports5["job"] = "主公";
//方式二
var airports6 = ["job": "将军", "age": "16", "name": "zhangfei"];
airports6.updateValue("主公", forKey: "job");
print(airports)//!< ["age": "16", "job": "主公", "name": "zhangfei"]
if let oldValue = airports6.updateValue("男", forKey: "sex") {
print("key早就已经存在了,并且旧值为\(oldValue)");
} else {
print("key不存在,需添加到字典中");
//!< log:key不存在,需添加到字典中
}
// remove
//方式一.使用下标语法
var airports7 = ["job": "将军", "age": "16", "name": "zhangfei"];
airports7["job"] = nil;
//方式二
if let value = airports7.removeValue(forKey: "job") {
print("The value \(value) was removed.");
//!< The value 将军 was removed.
}
print(airports);//!< ["age": "16", "name": "zhangfei"]
// 遍历
//! 方式一
for item in airports7 {
print("\(item.key)");
print("\(item.value)");
}
//! 方式二
for (key,value) in airports7 {
print("\(key)");
print("\(value)");
}
for key in airports7.keys {
print(key);
}
for value in airports7.values {
print(value);
}
let keysArray = [String](airports.keys);
let valuesArray = Array<String>.init(airports.values);