數(shù)組用法
//初始化數(shù)組
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]
//給數(shù)組倒序 注意此時下標(biāo)的值沒有變化
testArray.reverse()
for i in testArray {
print("每個元素值為:\(i)")
}
for (index,value) in testArray.enumerated() {
print("下標(biāo) = \(index), 值 = \(value)")
}
testArray.append(10)//追加元素
print("testArray = \(testArray)")
if testArray.isEmpty {
print("判斷是否為空")
}
let count = testArray.count
print("數(shù)組元素個數(shù) = \(count)")
testArray += [21] //使用+=必須里面的類型一致
print(testArray)
//修改元素
var newArray = ["hello","you","jk","test"]
newArray[0] = "修改了第0個元素"
//根據(jù)下標(biāo)范圍修改
newArray[2...4] = ["范圍2","范圍4"]
newArray[2..<4] = ["范圍2","范圍4"]
//在對應(yīng)的位置插入元素
newArray.insert("我是插入的元素", at: 2)
//在某個位置插入新的數(shù)組
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是否保存內(nèi)存空間
_ = newArray.removeAll(keepingCapacity: true)
print(newArray.capacity)
集合Set用法
Set中的類型必須是Hashable的
let letters = Set<String>()
let count = letters.count
print(count)
//Set 無序且相同元素只出現(xiàn)一次
let set:Set<String> = ["你好","hello","你好","name","hhh","cad","name"]
print("\(set)")
//重復(fù)的不會輸出["name", "hhh", "你好", "hello", "cad"]
set.insert("我是插入的")
if set.isEmpty {
print("集合為空")
}
//因為一個集合不是一個有序集合沦疾,所以“first”元素可能不是第一個添加到集合中的元素称近。 該集合不能為空。
let removeFirst = set.removeFirst()
//同理數(shù)組的其他幾個也一樣哮塞,在集合里面
// Set 的remove 返回的是一個可選值
if let removed = ingredients.remove("hhh") {
print("The hhh is now delete")
}
if set.contains("你好") {
print("參數(shù)是否在集合中")
}
//遍歷
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()方法忆畅,該方法將集合的元素作為使用<運算符排序的數(shù)組衡未。"
for gen in set.sorted() {
print(gen)
}
//集合本身的forEach
set.forEach { (value) in
print(value)
}
//創(chuàng)建一個只有兩個集合通用(交集)的值的新集合
intersection(_:)
//在集合中創(chuàng)建一個新集合,但不能同時創(chuàng)建集合
symmetricDifference(_:)
//用兩個集合中的所有值創(chuàng)建一個新集合
union(_:)
//創(chuàng)建一個新的集合家凯,其值不在指定的集合中
subtracting(_:)
//
let set2:Set<String> = ["你好","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("字典的數(shù)量\(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構(gòu)造數(shù)組
let keysArray = Array(keys)
print(keysArray)