数组用法
//初始化数组
let emptyArray = [String]()
//Array初始化4个一样的字符串,repeating 是Array.Element 类型
let f4Doubles = Array(repeating: "hello", count: 4)
//输出[1, 2, 3, 4, 5, 6, 7] 使用范围初始化
let numbers = Array(1...7)
//一般使用到的初始化
let array1:[String] = ["3","6","9","4"]
let array2:[Any] = ["2",3,"e",4]
var appendArray = array1 + array2//出错
//只有类型一样才可以+
let numbers1 = Array(1...3)
let numbers2 = Array(5...8)
let ss = numbers1 + numbers2
//一般用法
var testArray = [2,5,3,1,9,6,4,8,7]
//给数组倒序 注意此时下标的值没有变化
testArray.reverse()
for i in testArray {
print("每个元素值为:\(i)")
}
for (index,value) in testArray.enumerated() {
print("下标 = \(index), 值 = \(value)")
}
testArray.append(10)//追加元素
print("testArray = \(testArray)")
if testArray.isEmpty {
print("判断是否为空")
}
let count = testArray.count
print("数组元素个数 = \(count)")
testArray += [21] //使用+=必须里面的类型一致
print(testArray)
//修改元素
var newArray = ["hello","you","jk","test"]
newArray[0] = "修改了第0个元素"
//根据下标范围修改
newArray[2...4] = ["范围2","范围4"]
newArray[2..<4] = ["范围2","范围4"]
//在对应的位置插入元素
newArray.insert("我是插入的元素", at: 2)
//在某个位置插入新的数组
newArray.insert(contentsOf: ["111","222"], at: 3)
let cast = ["Vivien", "Marlon", "Kim", "Karl"]
let list = cast.joined(separator: ", ")//list这时候返回的是一个字符串
// Prints "Vivien, Marlon, Kim, Karl"
let nestedNumbers = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
let joined = nestedNumbers.joined(separator: [-1, -2])
//这时候joined是JoinedSequence序列类型
print(Array(joined))
//[1, 2, 3, -1, -2, 4, 5, 6, -1, -2, 7, 8, 9]
//也可以通过范围来joined
let ranges = [0..<3, 8..<10, 15..<17]
for index in ranges.joined() {
print(index, terminator: " ")
}
// Prints: "0 1 2 8 9 15 16"
let removeItem = newArray.remove(at: 2)
print("被删除的元素为:\(removeItem)")
//删除第一个元素
let removeFirst = newArray.removeFirst()
//删除第二个元素
let removeLast = newArray.removeLast()
//删除前3个元素
newArray.removeFirst(3)
//删除后3个元素
newArray.removeLast(3)
//删除1...4的元素
newArray.removeSubrange(1...4)
//删除全部 keepingCapacity是否保存内存空间
_ = newArray.removeAll(keepingCapacity: true)
print(newArray.capacity)
集合Set用法
Set中的类型必须是Hashable的
let letters = Set()
let count = letters.count
print(count)
//Set 无序且相同元素只出现一次
let set:Set = ["你好","hello","你好","name","hhh","cad","name"]
print("\(set)")
//重复的不会输出["name", "hhh", "你好", "hello", "cad"]
set.insert("我是插入的")
if set.isEmpty {
print("集合为空")
}
//因为一个集合不是一个有序集合,所以“first”元素可能不是第一个添加到集合中的元素。 该集合不能为空。
let removeFirst = set.removeFirst()
//同理数组的其他几个也一样,在集合里面
// Set 的remove 返回的是一个可选值
if let removed = ingredients.remove("hhh") {
print("The hhh is now delete")
}
if set.contains("你好") {
print("参数是否在集合中")
}
//遍历
for gen in set {
print(gen)
}
//"Swift’s Set type does not have a defined ordering. To iterate over the values of a set in a specific order, use the sorted() method, which returns the set’s elements as an array sorted using the < operator"
//"Swift的Set类型没有定义的顺序。 要按特定顺序迭代集合的值,请使用sorted()方法,该方法将集合的元素作为使用<运算符排序的数组。"
for gen in set.sorted() {
print(gen)
}
//集合本身的forEach
set.forEach { (value) in
print(value)
}
//创建一个只有两个集合通用(交集)的值的新集合
intersection(_:)
//在集合中创建一个新集合,但不能同时创建集合
symmetricDifference(_:)
//用两个集合中的所有值创建一个新集合
union(_:)
//创建一个新的集合,其值不在指定的集合中
subtracting(_:)
//
let set2:Set = ["你好","name","hhh"]
let set3:Set = ["qp","ss","lo"]
let isTrue = set2.isSubset(of: setEqual)
if isTrue {
print("一个集合的所有值包含在指定的集合中")
}
let isTrue2 = setEqual.isSuperset(of: set2)
if isTrue2 {
print("一个集合包含指定集合中的所有值")
}
//isStrictSubset(of :)或isStrictSuperset(of :)方法来确定一个集合是一个子集还是超集,但不等于一个指定的集合
let isTure3 = set2.isStrictSubset(of: setEqual)
if isTure3 {
print("set2 是 setEqual的子集")
}
let isTure4 = setEqual.isStrictSuperset(of: set2)
if isTure4 {
print("setEqual 是 set2的超集")
}
let isTure5 = set3.isDisjoint(with: set)
if isTure5 {
print("确定两个集合没有共同的值")
}
字典用法
var testDic = [Int: String]()
testDic[12] = "hello"
testDic[9] = "world"
testDic = [:]
if testDic.isEmpty {
print("字典为空")
}
print(testDic)
var testDic2:[String: String] = ["token":"221212","type":"21"]
print(testDic2)
let count = testDic2.count
print("字典的数量\(count)")
//赋值
testDic2["method"] = "test"
//修改
testDic2["type"] = "100"
print(testDic2)
//updateValue 方法
if let oldValue = testDic2.updateValue("newValue", forKey: "token") {
print("获取到的旧值:\(oldValue)")
}
print(testDic2)
//删除值
testDic2["type"] = nil
print(testDic2)
if let removeValue = testDic2.removeValue(forKey: "token") {
print("被移除的值\(removeValue)")
}
print(testDic2)
//删除所有
testDic2.removeAll()
let testDic3:[String: String] = ["token":"221212","type1":"hsaj","type2":"hej","type3":"21","type4":"109"]
for (keyv,valuek) in testDic3 {
print("key = \(keyv) value = \(valuek)")
}
//返回所有的key
let keys = testDic3.keys
for key in keys {
print(key)
}
//返回所有的values
let values = testDic3.values
for value in values {
print(value)
}
//通过keys构造数组
let keysArray = Array(keys)
print(keysArray)