介绍了字符、字符串、元组、可选型,这篇介绍Swift语言的容器类Collections,数组、字典、集合,他们有各自的特点,数组是有序的;集合是无序的,且具有唯一性,提供集合操作,特殊的计算、快速查找;字典是以键值对的形式存在,我们在实际使用过程中要选择合适的数据结构;对一个数据结构的学习就是学习它的增、删、改、查。
数组
//: Playground - noun: a place where people can play import UIKit //声明一个数组 var numbers = [2,3,4,5,6] //默认是String var vowels = ["T","E","D","K"] var numbers1: [Int] = [2,6] //默认是String var vowels1: [String] = ["T","K"] var numbers2: Array<Int> = [2,6] //默认是String var vowels2: Array<String> = ["T","K"] //声明一个空的数组 var emptyArray1: [Int] = [] var emptyArray2: Array<Int> = [] var emptyArray3 = [Int]() var emptyArray4 = Array<Int>() //使用构造方法声明数组 var allZeros = [Int](count: 6,repeatedValue: 0) var allZeros1 = Array<Int>(count: 6,repeatedValue: 0) //不建议用这种方式声明数组 var array = [] //数组的基本用法 查 vowels.count numbers.isEmpty emptyArray1.isEmpty vowels[3] //得到的是一个可选型,因为数组有可能为空,得到的就是nil vowels.first vowels.last emptyArray1.first //自动解包 if let first = emptyArray1.first{ print("emptyArray1 first") } if let first = vowels.first{ print("vowels first = \(first)") } //强制解包 vowels.first! //也可以通过下标得到最后一个元素 vowels[vowels.count-1] //得到数组中的最小值 numbers.minElement() vowels.maxElement() //得到数组的一个范围值 numbers[1..<3] numbers[2..<numbers.count] //是否包含 vowels.contains("T") vowels.contains("C") //得到字符的索引值 vowels.indexOf("E") vowels.indexOf("L") //遍历数组 for index in 0..<numbers.count{ print("numbers index \(index) is \(numbers[index])") } //推荐使用 for number in numbers{ print("number is \(number)") } //需要使用元组下标可以使用以下方法 for (index,vowel) in vowels.enumerate(){ print("numbers index \(index) is \(vowel)") } //判断数组是否相等 var nums = [2,6]; numbers == nums //数组的更多操作 增 删 改 var courses = ["第一课","第二课","第三课"] courses.count //添加一个元素 courses.append("第四课") courses.count print(courses) //另一个添加元素的方法,=号的另一边要是同类型才能相加 courses += ["第五课"] courses.count print(courses) //插入指定位置 courses.insert("新添加",atIndex: 2) print(courses) //删除 courses.removeFirst() print(courses) courses.removeLast() print(courses) courses.removeAtIndex(2) print(courses) courses.removeRange(0..<2) print(courses) courses.removeAll(); print(courses) courses.append("第一课") courses.append("第二课") courses.append("第三课") courses.append("第四课") courses.append("第五课") print(courses) //改 courses[0] = "lesson1" print(courses) //修改1〜3的下标,如果没有就删除对应下标 courses[1...3] = ["lesson2","lesson3"] print(courses) courses[0..<3] = ["lensson"] print(courses) //二维数组 // 数组的定义以下三种方式都可以定义二维数组 var board = [[276,73,0],[456,45,49,[409,0]] var board1: [Array<Int>] = [[276,[675,0]] var board2: Array<Array<Int>> = [[276,0]] board[0] board[0][0] board.count board[0].count //NSArray NSArray与String的区别一个是引用,一个是值 var array2 = [2,6] as NSArray var array3: NSArray = [4,"Hello",8]字典的学习
//: Playground - noun: a place where people can play import UIKit //字典的介绍,字典是无序的数据结构 var dict = ["swift":"面向对象","C语言":"面向过程","java":"面向对象"] if let value = dict["swift"]{ print("swift is \(value)") } dict.count dict.isEmpty dict.first //得到所有的key Array(dict.keys) //得到所有的value Array(dict.values) for key in dict.keys{ print(key) } for value in dict.values{ print(value) } for (index,value) in dict.enumerate(){ print("index is \(index) value is \(value)") } for (key,value) in dict{ print("key is \(key) value is \(value)") } //声明空的字典 var emptyDic1: [String: Int] = [:] var emptyDic2: Dictionary<Int,String> = [:] var emptyDic3 = [String: String]() var emptyDic4 = Dictionary<Int,Int>() let dict1 = [1:"A",2:"B",3:"C"] let dict2 = [1:"A",3:"C",4:"D"] dict1 == dict2 //字典的增,删,改 var user = ["name":"dzt","pwd":"111","age":"30"] //修改 user["pwd"] = "000" print(user) user.updateValue("49",forKey: "age") print(user) let oldPwd = user.updateValue("000",forKey: "pwd") //判断修改前与修改后的密码是否一致 if let oldPwd = oldPwd,newPwd = user["pwd"] where oldPwd == newPwd{ print("注意,修改后的密码与修改前密码一样") } //增, user["msg"] = "hello" print(user) user.updateValue("insert",forKey: "title") print(user) //删除 user["title"] = nil print(user) if let msg = user.removeValueForKey("msg"){ print("msg is \(msg) delete success") } user.removeAll() print(user)
集合的学习
//: Playground - noun: a place where people can play import UIKit //集合的使用,集合是无序的,可以保证数据是唯一的 var lang: Set<String> = ["java","swift"] //声明一个空集合 var emptySet1: Set<Int> = [] var emptySet2 = Set<Int>() //可以有多种声明方法 var vowels = Set(["A","I","I"]) var langs: Set = ["Swift","java","C#"]; lang.count lang.isEmpty emptySet1.isEmpty //随机取出的数 let e = lang.first lang.contains("swift") for l in lang{ print(l) } lang == langs //集合的增删改 var skillsA: Set<String> = ["java","swift"] var skillsB: Set<String> = ["javascript","oc","html"] var skillsC: Set<String> = [] //增 skillsC.insert("oc") skillsC.insert("css") skillsC.insert("swift") skillsC.insert("oc") print(skillsC) //删除 skillsC.remove("oc") print(skillsC) if let skill = skillsC.remove("css"){ print("\(skill) is delete") } skillsC.removeAll() //计算并集 skillsA.union(skillsB) print(skillsA) //skillsA.unionInPlace(skillsB) //print(skillsA) //交集 skillsA.intersect(skillsB) //集合减法 skillsA.subtract(skillsB) //集合异或 skillsA.exclusiveOr(skillsB) skillsA.union(["java","c#"]) print(skillsA) //判断字集 var skillsD: Set = ["swift"] skillsD.isSubsetOf(skillsA) skillsD.isStrictSubsetOf(skillsA) //判断超集 skillsA.isSupersetOf(skillsD) skillsA.isStrictSupersetOf(skillsD) //判断是否相离,没有公共部分 skillsA.isDisjointWith(skillsB) skillsA.isDisjointWith(skillsC)以上代码都是在Xcode的Playground上运行的。 原文链接:https://www.f2er.com/swift/322833.html