Swift集合类型(Collection Types)

8 篇文章 0 订阅
本文介绍了Swift中的两种主要集合类型——数组和字典的操作,包括初始化、遍历、添加、删除、修改元素等。示例中展示了如何使用数组的append、insert、remove等功能,以及字典的更新、移除、遍历键值等方法。
摘要由CSDN通过智能技术生成
// 数组
var shoppingList = ["Eggs", "Milk"]

shoppingList.count  //  shoppingList.count = 2

if shoppingList.isEmpty {
    println("The shopping list is empty")
} else {
    println("The shopping list is not empty")
}

// The shopping list is not empty

shoppingList.append("Flour")

shoppingList // ["Eggs", "Milk", "Flour"]

shoppingList += ["Cheese", "Butter"]
// "Eggs", "Milk", "Flour", "Cheese", "Butter"]

// 用索引获取元素
var firstItem = shoppingList[0]
firstItem  // Eggs
// 改变数组中一些列元素
shoppingList[1...3] = ["Bananas", "Apples"]
shoppingList  // ["Eggs", "Bananas", "Apples", "Butter"]

// 插入元素
shoppingList.insert("Maple Syrup", atIndex: 0)
shoppingList // ["Maple Syrup", "Eggs", "Bananas", "Apples", "Butter"]

// 按索引删除元素,并返回该被删除的元素,当然有时候并不需要返回值
let mapleSyrup = shoppingList.removeAtIndex(0) // mapleSyrup = "Maple Syrup"

let last = shoppingList.removeLast() // last = "Butter"

// for in 遍历数组
for item in shoppingList {
    println(item)
}

// enumerate()返回数组的索引和值
for (index, value) in enumerate(shoppingList) {
    println("Item \(index + 1) : \(value)" )
}

//显示定义数组
var someInts = [Int]()

someInts.append(3)

//清空数组
someInts = []

// [1.0, 1.0, 1.0]
var threeDoubles = [Double](count: 3, repeatedValue: 1.0)

// 推断类型
// [2.2, 2.2, 2.2]
var anotherThreeDoubles = Array(count: 3, repeatedValue: 2.2)

// 用 + 可以把相同类型的数组相加, [1.0, 1.0, 1.0, 2.2, 2.2, 2.2]
var appendArray = threeDoubles + anotherThreeDoubles


// 字典,airports的类型为 Dictionary<String, String>
var airports: Dictionary<String, String> = ["TYO" : "Tokyo", "DUB" : "Bublin"]

//获取airports的元素个数
airports.count // 2

// 读取,修改字典,添加
airports["LHR"] = "London"
airports      // ["LHR": "London", "TYO": "Tokyo", "DUB": "Bublin"]

// 更新字典元素
if let oldValue = airports.updateValue("Dublin Internation", forKey: "DUB") {
    // "The old value for DUB was Bublin"
    println("The old value for DUB was \(oldValue)")
}

// 通过key,获得Dictionary 的value是,返回的是optiion类型,因为有可能该key不存在
// "The name of the airport is Dublin Internation"
if let airportName = airports["DUB"] {
    println("The name of the airport is \(airportName)")
} else {
    println("That airport is not in the airports dictionary")
}

// 移除元素
airports["DUB"] = nil

airports // ["LHR": "London", "TYO": "Tokyo"]


// "The removed airport's name is London"
if let removedValue = airports.removeValueForKey("LHR") {
    println("The removed airport's name is \(removedValue)")
} else {
    println("The airports dictionary does not contain a value for LHR")
}


// 添加元素
airports["LHR"] = "London"
airports["DUB"] = "Bublin"

// 遍历字典
//LHR: London
//TYO: Tokyo
//DUB: Bublin

for (airportCode, airportName) in airports {
    println("\(airportCode): \(airportName)")
}

// 遍历key
//key: LHR
//key: TYO
//key: DUB
for airportCode in airports.keys {
    println("key: \(airportCode)")
}

// 遍历value
//value: London
//value: Tokyo
//value: Bublin
for airportName in airports.values {
    println("value: \(airportName)")
}


// 用Dictionary的key或value创建数组
let airportCodes = Array(airports.keys)
let airportNames = Array(airports.values)

// 创建的空字典
var namesOfIntegers = Dictionary<Int, String>()

namesOfIntegers[16] = "sixteen"

// 清空字典
namesOfIntegers = [:] // 0 key/value pairs

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值