在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ Swift提供了三种主要的集合类型,称为数组,集合和字典,用于存储值集合。 数组是有序的值集合。 集是唯一值的无序集合。 字典是键值关联的无序集合。 Swift中无法再使用传统形式的for循环。 1 //传统for循环形式不适用于Swift 2 for(单次表达式;条件表达式;末尾循环体){中间循环体;} 遍历数组:可以使用 1 let arrayStr:[String] = ["s","t","r","e","n","g","t","h","e","n","!"] 2 3 //方法1:根据元素顺序遍历 4 for item in arrayStr { 5 print(item) 6 } 7 8 //方法2:根据部分元素顺序遍历 9 for item in arrayStr[1...5] { 10 print(item) 11 } 12 13 //方法3:根据索引顺序遍历 14 for index in 0...(arrayStr.count - 1) { 15 print(arrayStr[index]) 16 } 17 18 //方法4:初始化数组并顺序遍历 19 for item in Array(1...10) { 20 print(item) 21 } 22 23 //方法5:stride函数 24 //from,to,by 最后一个值是 >(或<) to的值 25 //from,through,by 最后一个值是 >=(或<=) through的值 26 for index in stride(from: 0, to: arrayStr.count,by: 1 ) 27 { 28 print(arrayStr[index]) 29 } 如果不需要序列中的每个值,则可以使用下划线(_)代替变量名来忽略这些值。 1 let base = 3 2 let power = 10 3 var answer = 1 4 for _ in 1...power { 5 answer *= base 6 } 7 print("\(base) to the power of \(power) is \(answer)") 8 // Prints "3 to the power of 10 is 59049" 遍历集合:可以使用 1 let arrayStr:Set<String> = ["s","t","r","e","n","g"] 2 //输出的顺序是随机的 3 for item in arrayStr { 4 print("\(item)") 5 } Swift的 1 let arrayStr:Set<String> = ["s","t","r","e","n","g"] 2 //按照字母排序顺序输出 3 for item in arrayStr.sorted() { 4 print("\(item)") 5 } 遍历字典(可单独遍历键、值、或键值) 可以用字典遍历键值对 1 var direc: [Int: String] = [0:"s",1:"t",2:"r",3:"e",4:"n",5:"g"] 2 3 //按键值对遍历顺序遍历 4 for (num, char) in direc.sorted(by: <) { 5 print("\(num): \(char)") 6 } 7 8 //按键遍历顺序遍历 9 for num in direc.keys.sorted(by: <) { 10 print("\(num)") 11 } 12 13 //按值遍历顺序遍历 14 for char in direc.values { 15 print("\(char)") 16 } 如果您需要使用带有 1 let nums = [Int](direc.keys) 2 print(nums) 3 //次序随机 [4, 5, 2, 0, 1, 3] 4 5 let chars = [String](direc.values) 6 print(chars) 7 //次序随机 ["n", "g", "r", "s", "t", "e"] Swift的 Swift中标准库中方法 1 //可以用来快速的枚举数组中的内容,并用元组(index, value)接收 2 public func enumerated() -> EnumeratedSequence<Array<Element>> 3 4 //可以倒叙数组内容 5 public func reversed() -> ReversedRandomAccessCollection<Array<Element>> 当两者结合使用用到索引时需要注意 1 let arrayStr:[String] = ["s","t","r","e","n","g","t","h","e","n","!"] 2 3 //索引元素值绑定一起倒序遍历 4 for i in arrayStr.enumerated().reversed() { 5 print("\(i) ", terminator: "") 6 } 7 /* 8 (offset: 10, element: "!") (offset: 9, element: "n") (offset: 8, element: "e") (offset: 7, element: "h") (offset: 6, element: "t") (offset: 5, element: "g") (offset: 4, element: "n") (offset: 3, element: "e") (offset: 2, element: "r") (offset: 1, element: "t") (offset: 0, element: "s") 9 */ 10 11 //只元素值倒序遍历,原索引不变 12 for i in arrayStr.reversed().enumerated() { 13 print("\(i) ", terminator: "") 14 } 15 /* 16 (offset: 0, element: "!") (offset: 1, element: "n") (offset: 2, element: "e") (offset: 3, element: "h") (offset: 4, element: "t") (offset: 5, element: "g") (offset: 6, element: "n") (offset: 7, element: "e") (offset: 8, element: "r") (offset: 9, element: "t") (offset: 10, element: "s") 17 */ 倒序遍历数组 1 let arrayStr:[String] = ["s","t","r","e","n","g","t","h","e","n","!"] 2 3 //方法1:根据元素倒序遍历 4 for item in arrayStr.reversed() { 5 print(item) 6 } 7 8 //方法2:根据部分元素倒序遍历 9 for item in arrayStr[1...5].reversed() { 10 print(item) 11 } 12 13 //方法3:根据索引倒序遍历 14 for index in (0...(arrayStr.count - 1)).reversed() { 15 print(arrayStr[index]) 16 } 17 18 //方法4:初始化数组并倒序遍历 19 for item in Array(1...10).reversed() { 20 print(item) 21 } 22 23 //方法4:初始化数组并倒序遍历 24 for item in Array(1...10).reversed() { 25 print(item) 26 } 27 28 //方法5:stride函数 29 //from,to,by 最后一个值是 >(或<) to的值 30 //from,through,by 最后一个值是 >=(或<=) through的值 31 for index in stride(from: arrayStr.count - 1, through: 0,by: -1 ) 32 { 33 print(arrayStr[index]) 34 } 倒序遍历集合 1 let arrayStr:Set<String> = ["s","t","r","e","n","g"] 2 //按照字母排序倒序输出 3 for item in arrayStr.sorted().reversed() { 4 print("\(item) ", terminator: "") 5 } 6 //Print "t s r n g e" 倒序遍历字典(可单独遍历键、值、或键值) 1 var direc: [Int: String] = [0:"s",1:"t",2:"r",3:"e",4:"n",5:"g"] 2 //按键值对倒序遍历 3 for (num, char) in direc.sorted(by: <).reversed() { 4 print("\(num): \(char)") 5 }
|
请发表评论