swift之数组(Array)、集合(Set)、字典(Dictionary)
最后更新于:2022-04-01 11:39:40
1、数组(Array):
~~~
import Foundation
//数组
//新建一个数组
var array1 = [Int]() //空数组 里面没有元素
print(array1.count)
array1.append(3) //添加元素
print(array1.count)
array1 = [] //清空数组
print(array1.count)
//新建一个带有默认值的数组
var array2 = [Float](count: 5, repeatedValue: 2.11)
print(array2)
var array3 = [Float](count: 2, repeatedValue: 3.22)
print(array3)
var array23 = array2 + array3 //合并两个类型相同的数组
print(array23)
//新建指定数值的数组
var array4 : [String] = ["value1", "value2", "value3"]
print(array4)
//访问和修改数组值
var array5 = [String]()
if array5.count == 0 {
print("array5 is empty")
}
if array5.isEmpty {
print("array5 is empty")
}
//添加数据项
array5.append("v1")
array5 += ["v2"]
array5 += ["v3", "v4"]
print(array5)
print(array5[0])
array5[0] = "v111" //改变值
print(array5)
array5[1...3] = ["v123"] //3项 替换为1项
print(array5)
array5.insert("v-insert", atIndex: 1) // 在某个具体索引之前添加数据项
print(array5)
array5.removeAtIndex(2) // 按索引移除某一项
print(array5)
//array5.removeFirst() 移除第一项
//array5.removeLast() 移除最后一项
array5.removeAll() //移除所有
print(array5)
//遍历数组
var array6 = [1, 2, 3, 4, 5, 6]
for item in array6 {
print(item)
}
for (index, value) in array6.enumerate() {
print("index:\(index),value:\(value)")
}
~~~
输出:
~~~
0
1
0
[2.11, 2.11, 2.11, 2.11, 2.11]
[3.22, 3.22]
[2.11, 2.11, 2.11, 2.11, 2.11, 3.22, 3.22]
["value1", "value2", "value3"]
array5 is empty
array5 is empty
["v1", "v2", "v3", "v4"]
v1
["v111", "v2", "v3", "v4"]
["v111", "v123"]
["v111", "v-insert", "v123"]
["v111", "v-insert"]
[]
1
2
3
4
5
6
index:0,value:1
index:1,value:2
index:2,value:3
index:3,value:4
index:4,value:5
index:5,value:6
Program ended with exit code: 0
~~~
2、集合(Set)
~~~
//set 集合
//用来存储相同数据类型,但是没有确定顺序,元素的值不能相同
var set1 = Set<String>()
set1.insert("value1")
set1.insert("value2")
print(set1)
set1.insert("value1")
print(set1)
set1 = [] //置空
print(set1)
//用数组创建集合
var set2 : Set<String> = ["set21", "set22", "set23"]
var set3 : Set<String> = ["set211", "set444", "set222", "set233", "set222"]
print(set2)
print(set3)
//set3.isEmpty
//set3.count //这两个和数组一样
set3.insert("set1")
print(set3)
set3.remove("set1")
/*
你可以通过调⽤ Set 的 remove(_:) ⽅法去删除⼀个元素,如果该值是该 Set 的⼀个元素则删
除该元素并且返回被删除的元素值,否认如果该 Set 不包含该值,则返回 nil 。另
外, Set 中的所有元素可以通过它的 removeAll() ⽅法删除。
*/
var isContain = set3.contains("set211")
//遍历一个set
for item in set3 {
print(item)
}
//排序后输出
for item in set3.sort() {
print(item)
}
//基本集合操作
var a : Set<Int> = [1, 3, 4, 6, 7, 8, 9]
var b : Set<Int> = [2, 4, 5, 8]
print(a)
print(b)
//交集
print(a.intersect(b))
//并集
print(a.union(b))
//a-b
print(a.subtract(b))
//并集-交集
print(a.exclusiveOr(b))
//集合成员关系和相等
var c : Set<Int> = [1, 3]
var d : Set<Int> = [1, 3]
if c == d {
print("c和d这两个集合相等")
}
if c.isSubsetOf(a) {
print("a包含c")
}
if a.isSupersetOf(d) {
print("a包含d")
}
if c.isDisjointWith(b) {
print("c与b没有交集")
}
if c.isStrictSubsetOf(a) {
print("c是a的子集合")
}
/*
/// Returns true if the set is a subset of a finite sequence as a `Set`.
@warn_unused_result
public func isSubsetOf<S : SequenceType where S.Generator.Element == Element>(sequence: S) -> Bool
/// Returns true if the set is a subset of a finite sequence as a `Set`
/// but not equal.
@warn_unused_result
public func isStrictSubsetOf<S : SequenceType where S.Generator.Element == Element>(sequence: S) -> Bool
*/
//看上面定义可知 这两个的区别是isStrictSubsetOf 两个集合不能相等 isSubsetOf可以相等
if c.isSubsetOf(d) {
print("--")
}
if c.isStrictSubsetOf(d){
print("++")
}
~~~
集合操作图:
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-05-05_572b19ff3d01c.jpg)
输出:
~~~
["value1", "value2"]
["value1", "value2"]
[]
["set22", "set21", "set23"]
["set211", "set222", "set233", "set444"]
["set1", "set211", "set222", "set233", "set444"]
set211
set222
set233
set444
set211
set222
set233
set444
[4, 9, 6, 7, 3, 1, 8]
[5, 2, 4, 8]
[4, 8]
[2, 4, 9, 6, 7, 5, 3, 1, 8]
[9, 6, 7, 3, 1]
[2, 9, 6, 7, 5, 3, 1]
c和d这两个集合相等
a包含c
a包含d
c与b没有交集
c是a的子集合
--
Program ended with exit code: 0
~~~
3、字典
~~~
//字典
/*
Swift 的字典使⽤ Dictionary<Key, Value> 定义,其中 Key 是字典中键的数据类型,Value是字典中对应于这些键所存储值的数据类型。
注意: ⼀个字典的 Key 类型必须遵循 Hashable 协议,就像 Set 的值类型。
我们也可以⽤ [Key: Value] 这样快捷的形式去创建⼀个字典类型。虽然这俩种形式功能上相同,但是后者是⾸选
*/
//两种创建方式
var dict1 = [String : String]()
var dict2 = Dictionary<String, String>()
print(dict1)
print(dict2)
dict1["key1"] = "value1"
print(dict1)
dict1 = [:] //置空
print(dict1)
var dict3 : [String : String] = ["key1":"value1", "key2":"value2", "key3":"value3"]
print(dict3)
//定义dict3的时候 [String : String] 可以省略
//字典基本操作
//dict3.isEmpty
//dict3.count 判断是否为空
//增加
dict3["key4"] = "value4"
print(dict3)
//修改
dict3["key4"] = "value4444444"
print(dict3)
dict3.updateValue("valueNew4", forKey: "key4")
print(dict3)
//删除
dict3.removeValueForKey("key4")
print(dict3)
//遍历
for (key, value) in dict3 {
print("\(key) : \(value)")
}
for key in dict3.keys {
print(key)
}
for value in dict3.values {
print(value)
}
for key in dict3.keys.sort() {
print(dict3[key]!)
}
~~~
输出:
~~~
[:]
[:]
["key1": "value1"]
[:]
["key1": "value1", "key3": "value3", "key2": "value2"]
["key1": "value1", "key4": "value4", "key2": "value2", "key3": "value3"]
["key1": "value1", "key4": "value4444444", "key2": "value2", "key3": "value3"]
["key1": "value1", "key4": "valueNew4", "key2": "value2", "key3": "value3"]
["key1": "value1", "key2": "value2", "key3": "value3"]
key1 : value1
key2 : value2
key3 : value3
key1
key2
key3
value1
value2
value3
value1
value2
value3
Program ended with exit code: 0
~~~