文章目录
  1. 1. Swift学习-04 数组与字典
    1. 1.1. 数组的初始化
    2. 1.2. 数组的基本操作
    3. 1.3. 字典的初始化
    4. 1.4. 字典的基础操作
    5. 1.5. 字典实战和UIKit初探

Swift学习-04 数组与字典

数组的初始化

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
//---------------4.1 数组的初始化---------------//
/**
* Arrays
* var array = ["A", "B", "C"]
* 一个数组只能存储特定类型的数据
* 所存储的数据不一定是一个类的对象,可以是基础数据类型
* 显示数组数据类型
* [String]
* Array<String>
*/
/**
var array = ["A", "B", "C", "D", "E", "F"]
var array2 = ["A", "B", "C", "D", "E", "F"]
var array3 = ["A", "B", "C", "D", "E", "F"]
array[0] = "AA"
array
//创建空数组
var array4 = [Int]()
//类似
var str = String()
var array5 = Array<String>()
//var array6:[Int] = []
var array6:Array<Int> = []
array6.append(2)
//数组清空
array = [];
array.append("A")
array
array = [String]()
array = Array<String>()
array6 = [Int](count: 10, repeatedValue: 0)
var array7 = [2, 3, 4]
var array8 = array6 + array7
array8
*/

数组的基本操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//---------------4.2 数组的基本操作---------------//
/**
var array = ["A", "B", "C", "D", "E", "F"]
array.count
array.isEmpty
array.append("G")
array += ["H", "I", "J", "K"]
//添加任意位置
array.insert("Hello", atIndex: 0)
array.removeAtIndex(0)
array
//快速删除最后元素
array.removeLast()
array
array.removeRange(Range<Int>(start: 0, end:1))
array.insert("A", atIndex: 0)
array[0]
//批量修改
array[2...4] = ["CC", "DD", "EE"]
array
//修改可以只指定一个
array[2...4] = ["Hello"]
array
//遍历数组
for index in 0..<array.count
{
println(array[index])
}
//拿不到索引值
for item in array
{
println(item)
}
//获取索引值
for (index,item) in enumerate(array)
{
println("\(index) - \(item)")
}
*/

字典的初始化

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
//---------------4.3 字典的初始化---------------//
/**
* Dictionarys
* var dictionary = [1:"A", 2:"B", 3:"C"]
* 字典存储的数据是键和值的数据对儿
* 所存储的数据中,键和值可以是任意数据类型
* 一个字典只能存储固定一种键和值的数据类型搭配
*/
/**
//显示类型声明字典
Dictionary<Int,String>()
Dictionary<String,String>()
var courses = [76:"慕课网2048私人订制" , 77:"CSS3 3D 特效" , 127:"玩儿转Swift"]
var websites = ["搜索引擎":"Google" , "学习网站":"慕课网" , "新闻网站":"网易新闻"]
var courses2:Dictionary<Int,String> = [76:"慕课网2048私人订制",77:"CSS3 3D 特效" , 127:"玩儿转Swift"]
var websites2:Dictionary<String,String> = ["搜索引擎":"Google" , "学习网站":"慕课网" , "新闻网站":"网易新闻"]
var courses3:[Int:String] = [76:"慕课网2048私人订制",77:"CSS3 3D 特效" , 127:"玩儿转Swift"]
var websites3:[String:String] = ["搜索引擎":"Google" , "学习网站":"慕课网" , "新闻网站":"网易新闻"]
//空字典
var dict = Dictionary<Int,String>()
var dict2 = [Int:String]()
//清空字典
websites = Dictionary<String,String>()
websites = [String:String]()
websites = [:]
*/

字典的基础操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//---------------4.4 字典的基础操作---------------//
/**
var courses = [76:"2048私人订制" , 77:"CSS3 3D 特效" , 127:"玩儿转Swift"]
var websites = ["搜索引擎":"Google" , "学习网站":"慕课网" , "新闻网站":"网易新闻"]
courses.count
courses.isEmpty
//Optional
courses[76]
websites["搜索引擎"]
"姓名:" + courses[76]!
//插入
courses[73] = "只学有用的"
courses
//修改
courses[76] = "hhhhhh"
courses
var oldValue = courses.updateValue("2000", forKey: 76)
courses
//删除一个元素
courses[73] = nil
courses
courses[73] = "QingU"
courses
let oldValue2 = courses.removeValueForKey(73)
oldValue2
courses
//遍历字典
for (key, value) in courses
{
println("\(key): \(value)")
}
//遍历键
courses.keys
courses.values
for key in courses.keys
{
println(key)
}
//强制转换数组
Array(courses.keys)
*/

字典实战和UIKit初探

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
//---------------4.6 字典实战和UIKit初探---------------//
/**
let colors =
[
"Air Force Blue":(red:93, green:138, blue:168),
"Bittersweet":(red:254, green:111, blue:94),
"Canary Yellow":(red:255, green:239, blue:0),
"Dark Orange":(red:255, green:140, blue:0),
"Electric Violet":(red:143, green:0, blue:255),
"Fern":(red:113, green:188, blue:120),
"Gamboge":(red:228, green:155, blue:15),
"HollWood Cerise":(red:244, green:0, blue:161),
"Icterine":(red:252, green:247, blue:94),
"Jazzberry Jam":(red:165, green:11, blue:94)
]
var backView = UIView (frame: CGRectMake(0, 0, 320, CGFloat(colors.count * 50)))
backView.backgroundColor = UIColor.blackColor()
backView
var index = 0
for (colorName,rgbTuple) in colors
{
var colorStripe = UILabel(frame: CGRectMake(0, CGFloat(index*50+5), 320, 40))
colorStripe.backgroundColor = UIColor(
red: CGFloat(rgbTuple.red)/255,
green: CGFloat(rgbTuple.green)/255,
blue: CGFloat(rgbTuple.blue)/255,
alpha: 1.0
)
colorStripe
var colorNameLabel = UILabel(frame: CGRectMake(0, 0, 300, 40))
colorNameLabel.font = UIFont(name: "Arial", size: 24)
colorNameLabel.textColor = UIColor.blackColor()
colorNameLabel.textAlignment = NSTextAlignment.Right
colorNameLabel.text = colorName
colorStripe.addSubview(colorNameLabel)
backView.addSubview(colorStripe)
index++
}
backView
*/
文章目录
  1. 1. Swift学习-04 数组与字典
    1. 1.1. 数组的初始化
    2. 1.2. 数组的基本操作
    3. 1.3. 字典的初始化
    4. 1.4. 字典的基础操作
    5. 1.5. 字典实战和UIKit初探