Swift--数组,字典,Set, 下标--03

2017-07-27  本文已影响525人  XieHenry

Swift语言的集合类型:
1.有序可重复---数组(Array)
2.无序不重复---集合 (Set)
3.无序可重复,但每个值有唯一的键(key)---字典 (Dictionary)
批量处理集合中的元素,可以使用for in 循环。

----数组----

1.数组的常用操作

//定义:Array <类型> 或 [类型]

//隐式初始化
var a = ["A","E","I","O","U"]
//显式初始化
var b : [Int] = [1,2,3,4,5]
var c : Array<Int> = [1,2,3,4,5]

//初始化空数组
var emptyArray1: [Int] = []
var emptyArray2: Array<Int> = []
var emptyArray3 = [Int]()

//注意:声明的array类型是NSArray
//var array = []

//重复10次,元素是3
let array1 = [Int](repeatElement(3, count: 10))

//创建一个有序范围的Int数组   Array(起始值...终止值)
let array2 = Array(0...10)

var addressArray = ["北京","上海"]
//对一个数组中添加元素
addressArray.append("武汉")

let otherAddress = ["天津","郑州"]

//两个数组合并为一个数组   +=
addressArray = addressArray + otherAddress


//插入xx到第几个
addressArray.insert("南京", at: 5)

//移除
addressArray.remove(at: 2)
print(addressArray)  //["北京", "上海", "天津", "郑州", "南京"]


//移除全部
addressArray.removeAll()

2.数组的增删改查

//获取数组第一个元素和最后一个元素
var addressArray = ["北京", "上海", "天津", "郑州", "南京","濮阳","深圳"]
addressArray.first
addressArray.last

//判断数组是否包含某个元素,返回一个布尔量
addressArray.contains("郑州")  //true

//判断数组是否包含某个元素,同时也知道元素所在索引位置,返回一个Int量,没有的话返回nil
addressArray.index(of: "郑州")


//遍历数组
for index in 0..<addressArray.count {
    print(addressArray[index])
}


//方法2,不需要获取索引
for number in addressArray{
    print(addressArray)
}


addressArray.removeSubrange(0...1)

//删除第一个元素
addressArray.removeFirst()
//删除最后一个元素
addressArray.removeLast()
//删除指定一个元素
addressArray.remove(at: 1)


//通过索引获取到元素直接修改
addressArray[0] = "Object-C"


//通过索引范围获取到一个子数组进行修改值
addressArray[0...1] = ["Object-C2","Object-C3"]

----Set----

1. Set的常用操作

//集合需要显示申明出来
//申明一个空集合
var emptySet1 : Set<Int> = []
var emptySet2 = Set<Double>()

//集合可以去重
var skillOfA : Set<String> = ["OC","Swift","OC"]  //{"OC", "Swift"}
var skillOfB : Set = ["H5","CSS","Java"]

//判断集合是否为空集合
skillOfB.isEmpty    //false
emptySet1.isEmpty  //true

//集合元素个数
skillOfB.count

//数组强制转成集合
var a = Set(["A","B","C","D","E","E"])

var A : Set <String> = ["A","B","C","D","E"]
//转换为数组:sorted
let C = A.sorted()


//判断某个集合是否包含某个元素,返回一个布尔量
skillOfA.contains("Swift")  //true
skillOfB.contains("swift")  //false

/****遍历集合******/
for skill in skillOfB{
    print(skill)
}

/****集合比较******/
let A : Set = [1,2,3]
let B : Set = [1,2,3,1,1,2,2,3,3]

A == B  //true

增删

var A : Set <String> = ["A","B","C","D","E"]
//增
A.insert("F")
//删
A.remove("B")
A.removeAll()

交 补 并 差

//需要一个个测试,如果打开其中一个,请先注释其他的操作
var x : Set = [1,2,3,4]
var y : Set = [3,4,5,6]

//交集  intersection
x.intersection(y)  //3 4

//差集
x.subtract(y) //1 2

//并集
x.union(y)  // 5, 6, 2, 3, 1, 4

//补集
x.formSymmetricDifference(y) //5, 6, 2, 1

其他操作

var x : Set = [3,4]
var y : Set = [3,4,5,6]

//子集:isSubset(可以相等)  严格子集
x.isSubset(of: y)
x.isStrictSuperset(of: y)

//无交集:isDisjoint
x.isDisjoint(with: y)

----字典----

1.字典的常用操作

//****声明空字典4种方式****
var emptyDict:[String:Int] = [:]
var emptyDict1:Dictionary<Int,String> = [:]
var emptyDict2 = [String:String]()
var emptyDict4 = Dictionary<Int,Int>()

//隐式初始化
var airports1 = ["PVG":"Shanghai Pudong","CHU":"Dalian"];
//显式字典key-value类型初始化
var airports2 : [String:String] = ["PVG":"Shanghai Pudong","CHU":"Dalian"];
//显式字典类型以及key-value类型初始化
var airports3 : Dictionary<String,String> = ["PVG":"Shanghai Pudong","CHU":"Dalian"];

//添加或更新---没有的key会添加,有的会会更新
airports1["SHQ"] = "Hongqiao Airport"
airports1["CHU"] = "大连周水子机场"


//移除,用下标把值设为nil
airports1["PVG"] = nil

//字典集合个数
airports1.count

//判断字典是否为空字典
airports1.isEmpty    //false

//循环一个字典 for in ,因为键值对有2个元素,用元组变量
for (key,value) in airports1 {
    print(key,value)
}


//单独使用其中键或值,使用keys或values
for key in airports1.keys {
    print(key)
}

//把键值对分离成数组,用[数组类型](字典变量.key)   [数组类型](字典变量.value)

let codes = [String](airports1.keys)
let airName = [String](airports1.values)

增删改

var user = ["name":"姓名","password":"密码"]

//增加或修改
//方法1
user.updateValue("100000@qq.com", forKey: "email")

//方法2
user["email"] = "1111111@qq.com"

//删除
user["email"] = nil
user.removeValue(forKey: "name")

----下标----

下标是方法的一种,是访问集合 列表或者序列中的元素的快捷方式。 用法:实例名[索引]。 可以访问或设置其中元素,下标可以是多维或嵌套的。

//定义形式:一个名为sunscript的计算属性,可以忽略set(只读)
/*
subscript(index: 参数类型) -> 返回类型 {
    get {}
    set {}
}
*/


//常见的用法:字典,数组,集合等
var array = [1,2,3,4,5]
array[0]

var dict = ["a":"1","b":"2","c":"3"]
dict["b"]

//通过下标简化调用方法调用

struct Cycle {
    func area(radius : Double) -> Double {
        return Double.pi * pow(radius, 2)
    }
    
    subscript(radius :Double) -> Double {
        return Double.pi * pow(radius, 2)
    }
}


let cycle = Cycle()
cycle.area(radius: 3)
cycle[3]
上一篇下一篇

猜你喜欢

热点阅读