加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

swift2 集合类型

发布时间:2020-12-14 02:00:55 所属栏目:百科 来源:网络整理
导读:数组 创建一个空数组 var someInt = [Int]()print("someInt is of type [int] with (someInt.count) items") 创建有默认值的数组 var someInt = [Double](count: 3,repeatedValue: 0.0) // [0.0,0.0,0.0] 数组相加 var someInt = [Double](count: 3,repeate

数组


创建一个空数组


var someInt = [Int]()
print("someInt is of type [int] with (someInt.count) items")


创建有默认值的数组


var someInt = [Double](count: 3,repeatedValue: 0.0)
// [0.0,0.0,0.0]


数组相加


var someInt = [Double](count: 3,repeatedValue: 0.0)
var anotherInt = [Double](count: 3,repeatedValue: 2.5)
someInt += anotherInt
// [0.0,2.5,2.5]


修改数组值


someInt[3...5] = [1.0,2.0]
for num in someInt{
    print(num)
}

// 将3个2.5改为1.0 2.0,此时共有5个值


遍历数组


除了上面的方式,还可以在遍历的时候拿到值的索引

for (index,num) in someInt.enumerate(){
    print("item (index+1):(num)")
}



集合


创建一个集合


var myset = Set<Character>()
print(myset.count)
myset.insert("c")
print(myset.count)


数组字面值初始化集合


var favoriteGenres: Set<String> = ["Rock","Classical","Hip hop"]
// favoriteGenres被构造成含有三个初始值的集合


遍历集合


var favoriteGenres: Set<String> = ["Rock","Hip hop"]
// favoriteGenres被构造成含有三个初始值的集合
for genre in favoriteGenres {
    print("(genre)")
}


集合的关系操作







集合比较


使用“是否等”运算符(==)来判断两个集合是否包含相同的值。
使用isSubsetOf(_:)方法来判断一个集合中的值是否也被包含在另外一个集合中。
使用isSupersetOf(_:)方法来判断一个集合中包含的值是另一个集合中所有的值。
使用isStrictSubsetOf(_:)或者isStrictSupersetOf(_:)方法来判断一个集合是否是另外一个集合的子集合或者父集合并且和特定集合不相等。
使用isDisjointWith(_:)方法来判断两个结合是否不含有相同的值。


字典


创建字典


var mydic = [Int:String]()
mydic[1]="one"


数组字面值初始化字典


var airports: [String:String] = ["TYO": "Tokyo","DUB": "Dublin"]


读取和修改字典


var airports: [String:String] = ["TYO": "Tokyo","DUB": "Dublin"]

print("The dictionary of airports contains (airports.count) items.")
// 打印 "The dictionary of airports contains 2 items."(这个字典有两个数据项)


if airports.isEmpty {
    print("The airports dictionary is empty.")
} else {
    print("The airports dictionary is not empty.")
}
// 打印 "The airports dictionary is not empty.(这个字典不为空)"


airports["LHR"] = "London"
// airports 字典现在有三个数据项

airports["LHR"] = "London Heathrow"
// "LHR"对应的值 被改为 "London Heathrow

if let oldValue = airports.updateValue("Dublin Internation",forKey: "DUB") {
    print("The old value for DUB was (oldValue).")
}
// 输出 "The old value for DUB was Dublin."(DUB原值是dublin)

updateValue(forKey:)函数会返回包含一个字典值类型的可选值。
举例来说:对于存储String值的字典,这个函数会返回一个String?或者“可选 String”类型的值。
如果值存在,则这个可选值值等于被替换的值,否则将会是nil。


airports["APL"] = nil
// APL现在被移除了

另外,removeValueForKey方法也可以用来在字典中移除键值对。
这个方法在键值对存在的情况下会移除该键值对并且返回被移除的value或者在没有值的情况下返回nil:

if let removedValue = airports.removeValueForKey("DUB") {
    print("The removed airport's name is (removedValue).")
} else {
    print("The airports dictionary does not contain a value for DUB.")
}


字典遍历


for (airportCode,airportName) in airports {
    print("(airportCode): (airportName)")
}
// TYO: Tokyo
// LHR: London Heathrow


也可以通过访问它的keys或者values属性(都是可遍历集合)检索一个字典的键或者值:

for airportCode in airports.keys {
    print("Airport code: (airportCode)")
}
// Airport code: TYO
// Airport code: LHR

for airportName in airports.values {
    print("Airport name: (airportName)")
}
// Airport name: Tokyo
// Airport name: London Heathrow


如果我们只是需要使用某个字典的键集合或者值集合来作为某个接受Array实例 API 的参数,
可以直接使用keys或者values属性直接构造一个新数组:

let airportCodes = Array(airports.keys)
// airportCodes is ["TYO","LHR"]

let airportNames = Array(airports.values)
// airportNames is ["Tokyo","London Heathrow"]

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读