1、數組(Array):
~~~
import Foundation
//數組
//新建一個數組
var array1 = [Int]() //空數組 里面沒有元素
print(array1.count)
array1.append(3) //添加元素
print(array1.count)
array1 = [] //清空數組
print(array1.count)
//新建一個帶有默認值的數組
var array2 = [Float](count: 5, repeatedValue: 2.11)
print(array2)
var array3 = [Float](count: 2, repeatedValue: 3.22)
print(array3)
var array23 = array2 + array3 //合并兩個類型相同的數組
print(array23)
//新建指定數值的數組
var array4 : [String] = ["value1", "value2", "value3"]
print(array4)
//訪問和修改數組值
var array5 = [String]()
if array5.count == 0 {
print("array5 is empty")
}
if array5.isEmpty {
print("array5 is empty")
}
//添加數據項
array5.append("v1")
array5 += ["v2"]
array5 += ["v3", "v4"]
print(array5)
print(array5[0])
array5[0] = "v111" //改變值
print(array5)
array5[1...3] = ["v123"] //3項 替換為1項
print(array5)
array5.insert("v-insert", atIndex: 1) // 在某個具體索引之前添加數據項
print(array5)
array5.removeAtIndex(2) // 按索引移除某一項
print(array5)
//array5.removeFirst() 移除第一項
//array5.removeLast() 移除最后一項
array5.removeAll() //移除所有
print(array5)
//遍歷數組
var array6 = [1, 2, 3, 4, 5, 6]
for item in array6 {
print(item)
}
for (index, value) in array6.enumerate() {
print("index:\(index),value:\(value)")
}
~~~
輸出:
~~~
0
1
0
[2.11, 2.11, 2.11, 2.11, 2.11]
[3.22, 3.22]
[2.11, 2.11, 2.11, 2.11, 2.11, 3.22, 3.22]
["value1", "value2", "value3"]
array5 is empty
array5 is empty
["v1", "v2", "v3", "v4"]
v1
["v111", "v2", "v3", "v4"]
["v111", "v123"]
["v111", "v-insert", "v123"]
["v111", "v-insert"]
[]
1
2
3
4
5
6
index:0,value:1
index:1,value:2
index:2,value:3
index:3,value:4
index:4,value:5
index:5,value:6
Program ended with exit code: 0
~~~
2、集合(Set)
~~~
//set 集合
//用來存儲相同數據類型,但是沒有確定順序,元素的值不能相同
var set1 = Set<String>()
set1.insert("value1")
set1.insert("value2")
print(set1)
set1.insert("value1")
print(set1)
set1 = [] //置空
print(set1)
//用數組創建集合
var set2 : Set<String> = ["set21", "set22", "set23"]
var set3 : Set<String> = ["set211", "set444", "set222", "set233", "set222"]
print(set2)
print(set3)
//set3.isEmpty
//set3.count //這兩個和數組一樣
set3.insert("set1")
print(set3)
set3.remove("set1")
/*
你可以通過調? Set 的 remove(_:) ?法去刪除?個元素,如果該值是該 Set 的?個元素則刪
除該元素并且返回被刪除的元素值,否認如果該 Set 不包含該值,則返回 nil 。另
外, Set 中的所有元素可以通過它的 removeAll() ?法刪除。
*/
var isContain = set3.contains("set211")
//遍歷一個set
for item in set3 {
print(item)
}
//排序后輸出
for item in set3.sort() {
print(item)
}
//基本集合操作
var a : Set<Int> = [1, 3, 4, 6, 7, 8, 9]
var b : Set<Int> = [2, 4, 5, 8]
print(a)
print(b)
//交集
print(a.intersect(b))
//并集
print(a.union(b))
//a-b
print(a.subtract(b))
//并集-交集
print(a.exclusiveOr(b))
//集合成員關系和相等
var c : Set<Int> = [1, 3]
var d : Set<Int> = [1, 3]
if c == d {
print("c和d這兩個集合相等")
}
if c.isSubsetOf(a) {
print("a包含c")
}
if a.isSupersetOf(d) {
print("a包含d")
}
if c.isDisjointWith(b) {
print("c與b沒有交集")
}
if c.isStrictSubsetOf(a) {
print("c是a的子集合")
}
/*
/// Returns true if the set is a subset of a finite sequence as a `Set`.
@warn_unused_result
public func isSubsetOf<S : SequenceType where S.Generator.Element == Element>(sequence: S) -> Bool
/// Returns true if the set is a subset of a finite sequence as a `Set`
/// but not equal.
@warn_unused_result
public func isStrictSubsetOf<S : SequenceType where S.Generator.Element == Element>(sequence: S) -> Bool
*/
//看上面定義可知 這兩個的區別是isStrictSubsetOf 兩個集合不能相等 isSubsetOf可以相等
if c.isSubsetOf(d) {
print("--")
}
if c.isStrictSubsetOf(d){
print("++")
}
~~~
集合操作圖:

輸出:
~~~
["value1", "value2"]
["value1", "value2"]
[]
["set22", "set21", "set23"]
["set211", "set222", "set233", "set444"]
["set1", "set211", "set222", "set233", "set444"]
set211
set222
set233
set444
set211
set222
set233
set444
[4, 9, 6, 7, 3, 1, 8]
[5, 2, 4, 8]
[4, 8]
[2, 4, 9, 6, 7, 5, 3, 1, 8]
[9, 6, 7, 3, 1]
[2, 9, 6, 7, 5, 3, 1]
c和d這兩個集合相等
a包含c
a包含d
c與b沒有交集
c是a的子集合
--
Program ended with exit code: 0
~~~
3、字典
~~~
//字典
/*
Swift 的字典使? Dictionary<Key, Value> 定義,其中 Key 是字典中鍵的數據類型,Value是字典中對應于這些鍵所存儲值的數據類型。
注意: ?個字典的 Key 類型必須遵循 Hashable 協議,就像 Set 的值類型。
我們也可以? [Key: Value] 這樣快捷的形式去創建?個字典類型。雖然這倆種形式功能上相同,但是后者是?選
*/
//兩種創建方式
var dict1 = [String : String]()
var dict2 = Dictionary<String, String>()
print(dict1)
print(dict2)
dict1["key1"] = "value1"
print(dict1)
dict1 = [:] //置空
print(dict1)
var dict3 : [String : String] = ["key1":"value1", "key2":"value2", "key3":"value3"]
print(dict3)
//定義dict3的時候 [String : String] 可以省略
//字典基本操作
//dict3.isEmpty
//dict3.count 判斷是否為空
//增加
dict3["key4"] = "value4"
print(dict3)
//修改
dict3["key4"] = "value4444444"
print(dict3)
dict3.updateValue("valueNew4", forKey: "key4")
print(dict3)
//刪除
dict3.removeValueForKey("key4")
print(dict3)
//遍歷
for (key, value) in dict3 {
print("\(key) : \(value)")
}
for key in dict3.keys {
print(key)
}
for value in dict3.values {
print(value)
}
for key in dict3.keys.sort() {
print(dict3[key]!)
}
~~~
輸出:
~~~
[:]
[:]
["key1": "value1"]
[:]
["key1": "value1", "key3": "value3", "key2": "value2"]
["key1": "value1", "key4": "value4", "key2": "value2", "key3": "value3"]
["key1": "value1", "key4": "value4444444", "key2": "value2", "key3": "value3"]
["key1": "value1", "key4": "valueNew4", "key2": "value2", "key3": "value3"]
["key1": "value1", "key2": "value2", "key3": "value3"]
key1 : value1
key2 : value2
key3 : value3
key1
key2
key3
value1
value2
value3
value1
value2
value3
Program ended with exit code: 0
~~~
- 前言
- swift控制語句,for,while,repeat-while,if,switch
- swift之聲明常量和變量
- swift之數值類型雜談(數值)
- swift之 元組
- oc與swift混編,OC調用swift,swift調用OC
- swift之可選類型
- swift之數組(Array)、集合(Set)、字典(Dictionary)
- swift之switch續(元組,值綁定,where)
- swift之控制轉移語句,continue,break,fallthrough,return,帶標簽的語句
- swift之函數(functions)
- swift之閉包(closure)
- swift之枚舉
- swift之類和結構體
- swift之屬性
- swift之方法(Methods)