swift - 字典和集合

//: Playground - noun: a place where people can playjava

 

import UIKitandroid

 

var str = "Hello, playground"web

 

//字典 Dictionary (鍵值,數據對應的無序數據集合)swift

 

//聲明字典數組

var dict:[String : String] = ["swift":"雨燕","Python":"大蟒","java":"爪哇島","groovy":"時髦的"]安全

//var dict1:Dictionary<String,String> = ["swift":"雨燕","Python":"大蟒","java":"爪哇島","groovy":"時髦的"]數據結構

 

//空字典的聲明spa

var emptyDictionary1:[String:Int] = [:]orm

var emptyDictionAry2:Dictionary<Int, String> = [:]ip

var emptyDictionAry3 = [String:String]()

var emptyDictionary4 = Dictionary<Int,Int>()

 

print(dict["swift"])

 

dict["C++"]

 

dict.count

dict.isEmpty

emptyDictionary1.isEmpty

 

Array(dict.keys)

Array(dict.values)

 

for key in dict.keys {

    print(key)

}

for value in dict.values {

    print(value)

}

 

for (key, value) in dict {

    print("\(key):\(value)")

}

 

 

let dict1 = [1:"A",2:"B",3:"C"]

let dict2 = [1:"A",2:"B",3:"C"]

 

dict1 == dict2//字典是無序的

 

//字典的操做

 

var user = ["name":"bobobo","passwork":"liuyubo","occupation":"programmer"]

//字典元素的修改

user["occupation"] = "freelancer"

 

user.updateValue("imooc", forKey: "password")

 

let oldPassword = user.updateValue("imooc", forKey: "password")

if let oldPassword = oldPassword,

   let newPassword = user["password"] , oldPassword == newPassword {

    print("注意:修改後的密碼和以前同樣,可能致使安全問題")

    

}

 

//添加元素

user["email"] = "imooc@imooc.com"

user

 

user.updateValue("imooc.com", forKey: "website")

user

 

//刪除元素

user["website"] = nil

user

 

//user.removeValue(forKey: "email")

//user

 

if let email = user.removeValue(forKey: "email") {

    print("電子郵箱\(email) 刪除成功")

}

user.removeAll()

 

 

////集合 Set

//var skillsOfA : Set<String> = ["swift","oc","oc"]//集合自動去重,即集合中的元素是惟一的

//

//var emptySet1:Set<Int> = []

//var emptySet2 = Set<Double>()

//

//var vowels = Set(["A","E","I","O","U"])

//var skillsOfB:Set = ["HTML","CSS"]

//

////基本方法

//skillsOfA.count

//

//let set:Set<Int> = [2,2,2,2]

//set.count

//

//skillsOfA.isEmpty

//emptySet1.isEmpty

//

//let e = skillsOfA.first

//skillsOfA.contains("swift")

//

//for skill in skillsOfB {

//    print(skill)

//}

//

//let setA = [1,2,3]

//let setB = [3,2,1]

//

//setA == setB//無序,沒有重複的元素

 

//集合的相關操做

var skillsOfA: Set<String> = ["swift","OC"]

var skillsOfB: Set<String> = ["HTML","CSS","Javascript"]

var skillsOfC: Set<String> = []

 

skillsOfC.insert("swift")

skillsOfC.insert("HTML")

skillsOfC.insert("CSS")

 

skillsOfC.insert("CSS")

 

////刪除

//skillsOfC.remove("CSS")

//skillsOfC

//skillsOfC.remove("Javascript")

//skillsOfC

//

//if let skill = skillsOfC.remove("HTML") {

//    print("HTML is Removed")

//}

//

//skillsOfC.removeAll()

 

//並集 union  unionInPlace

skillsOfA.union(skillsOfC)//不改變skillsOfA

skillsOfA

 

//skillsOfA.formUnion(skillsOfC)//改變skillsOfA

//skillsOfA

 

//交集

skillsOfA.intersection(skillsOfC)

 

//減法

skillsOfA.subtract(skillsOfC)

skillsOfC.subtract(skillsOfA)

 

 

 

//異或

 

 

skillsOfA.union(["java","android"])

 

var skillsOfD:Set = ["swift"]

 

skillsOfD.isSubset(of: skillsOfA)

skillsOfD.isStrictSubset(of: skillsOfA)

 

skillsOfA.isSuperset(of: skillsOfD)

 

 

//總結,  選擇合適的數據結構

//數組:有序

//集合:無序,惟一性,提供集合操做,快速查找

//字典:鍵-值數據對

相關文章
相關標籤/搜索