- 数组:
Array
- 空数组(实例化)
var intsArray = [Int]()
- 创建任意长度默认值数组
- 合并数组(注意,合并数组时,数组内部的数据类型必须相同)
var str1:[Character] = Array(repeating: "h", count: 1)
var str2:[Character] = Array(repeating: "e", count: 1)
var str3:[Character] = Array(repeating: "l", count: 2)
var str4:[Character] = Array(repeating: "o", count: 1)
var newArray = str1 + str2 + str3 + str4
var string = String()
string.insert(contentsOf: newArray, at: string.startIndex)
- 已知数值快速实例化数组
var strArray:[String] = ["str1","str2","str3"]
- 不定义数组类型,Swift会根据数组中数据类型自行定义数组类型
var str = ["str1","str2"]
- 判断数组是否为空
var str:[String] = []
var isEmpty = str.isEmpty
- 添加数组元素
//添加最后一位元素
var str:[String] = []
str += ["str1"]
str += ["str2","str3"]
str.append("str6")
//在指定位置添加元素(需要注意数组越界问题)
str.insert("str0", at: 0)
str.insert(contentsOf: ["str4","str5"], at: 4)
- 移除数组元素(需要注意数组越界问题)
var str = ["str6","str1","str","str2","str4"]
str.removeFirst()
str.remove(at: 1)
str.removeLast()
- 取值(需要注意数组越界问题)
var str = ["str1","str2"]
var string = str[0]
- 改变对应位置,对应区间值(区间长度跟替换值长度不一致时,也可以替换,相应的会使数组长度发生改变)
var str = ["str1","str2","str3","str4","str5"]
str[0] = "str0"
str[1...str.count-1] = ["str1","str2","str3","str4"]
str[str.count-2...str.count - 1] = ["str3","str4","str5"]
- 遍历数组
var str = ["str1","str2","str3","str4"]
//只取值
for item in str {
print(item)
}
//取值以及值对应的索引
for (index,item) in str.enumerated() {
print("index is \(index) value is \"\(item)\"")
}
- 集合:
Set
(集合内的所有元素无序排列,且相同元素只存在一个) - 空集合(实例化)
var set = Set<String>()
- 利用数组实例化集合(此处我们发现只会有一个str1,这是因为集合的唯一性.注意,如果使用数组定义集合,则集合变量或常量名后,必须跟上集合类型,否则Swift会自动推断为数组类型,集合内数据类型可不写)
//例子1,指定strSet为集合类型,集合内值类型为String类型
var strSet:Set<String> = ["str0","str1","str1","str2"]
//例子2,指定strSet1为集合类型,集合内值类型不写明,根据其后数组内数据类型自行填充
var strSet1:Set = ["str0","str1","str1","str2"]
//例子3,不制定strSet2类型,strSet2在Swift中自动被判断为数组类型
var strSet2 = ["str0","str1","str1","str2"]
- 增删改查
var set:Set = [1,2,3,4,5]
//集合长度
set.count
//集合是否为空
set.isEmpty
//插入数值
set.insert(6)
//移除某个值,方法返回的是这个对应的值,若返回nil,则说明集合中不存在改数值
let removeValue = set.remove(2)
//判断集合中是否存在对应值
var isContainSix = set.contains(6)
//清空集合
set.removeAll()
- 遍历集合
var set:Set = [1,2,3,4,5]
//无序遍历
for numbers in set {
print(numbers)
}
//有序遍历
for numbers in set.sorted() {
print(numbers)
}
- 集合操作
var set1:Set = [1,2,3,4,5]
var set2:Set = [4,5,6,7,8]
//union(合并)
set1.union(set2)
//symmetricDifference(去除相同元素,并合并两个集合中剩下的不同元素)
set1.symmetricDifference(set2)
//intersection(留下相同元素)
set1.intersection(set2)
//subtracting(去除相同元素,只保留调起方法集合中剩下的元素)
set1.subtracting(set2)
-集合间关系
var numberLessThanTen:Set = [1,2,3,4,5,6,7,8,9]
var oddNumber:Set = [1,3,5,7,9]
var oddNumber1:Set = [1,3,5,7,9]
var numberGreaterThanTen:Set = [10,11,12,13]
//超集(集合中包含所有对比集合的数)
numberLessThanTen.isSuperset(of: oddNumber)
//子集(集合中的数,对比集合中全部都有)
oddNumber.isSubset(of: numberLessThanTen)
//相等(两个相等的集合,互为超集和子集)
oddNumber == oddNumber1
//互斥(没有共同元素)
numberLessThanTen.isDisjoint(with: numberGreaterThanTen)
- 字典(
Dictionary
,无序,键值对存在,一个键对应一个值,相同键只存在一个) - 空字典(实例化)
var timeDict = [String:Int]()
- 已知对应键值实例化
var timeDict:[String:Int] = ["hour":10,"min":40,"sec":01]
var timeDict1 = ["hour":10,"min":40,"sec":01]
- 字典操作(可以用已知键来取值,注意下面例子我输出时用了!强制解析,因为字典中,用[key]取值,取到的是可选类型的值)
var timeDict = ["hour":10,"min":40,"sec":01]
//updataValue(方法返回一个optional的对象,若返回nil,则代表没有对应的键值对,如果键值存在,则更新键对应的值,若不存在,在字典中新增一个键值对)
timeDict.updateValue(55, forKey: "min")
timeDict.updateValue(21, forKey: "day")
//取值
timeDict["hour"]
print("time is \(timeDict["hour"]!):\(timeDict["min"]!):\(timeDict["sec"]!)")
//便捷的改变键值,与update方法相同,若不存在该键,在字典中新增一个键值对
timeDict["hour"] = 11
timeDict["month"] = 2
print("today is \(timeDict["month"]!)-\(timeDict["day"]!) time is \(timeDict["hour"]!):\(timeDict["min"]!):\(timeDict["sec"]!)")
//删除值(返回一个可选类型的值,若存在,则字典中含有该键值对,并删除,若返回nil,则表示不存在该键值对)
var month = timeDict.removeValue(forKey: "month")
var year = timeDict.removeValue(forKey: "year")
- 遍历
var timeDict = ["hour":10,"min":40,"sec":01]
//遍历键值对,以元组形式返回
for (key,value) in timeDict {
print("key is \(key) value is \(value)")
}
//遍历所有的键
for value in timeDict.values {
print ("value is \(value)")
}
//遍历所有的值
for key in timeDict.keys {
print ("value is \(key)")
}