更新:2018.05.24
整理了一下demo:SwiftDemo
- Swift將OC中的大部分類都變成了結構體瘩欺,比如
Sring
,比如Array
拌牲,比如Dictionary
俱饿。 - 按住command點擊隨便一個
Int
或者Double
或者String
都會發(fā)現(xiàn)前面有一個struct
,結構體塌忽。使用起來變得更加簡單拍埠。 - Swift中不再存在OC中的
NSMutableArray
、NSMutableString
,NSMutableDictionary
在Swift中土居,只通過參數(shù)名前面是let
還是var
枣购,let
就是不可變嬉探,var
就是可變。
String
- Swfit中棉圈,String是一個有序的字符集合涩堤,字符串可以通過String類型表示,也可以表示為Character類型的集合分瘾。
let characters : [Character] = ["h","e","l","l","o"]
let hello01 = String(characters)
print(hello01)
- Swift中胎围,拼接和操作字符串的方式與C中相似,輕量且易讀德召,終于可以簡單的使用'+'連接
let name = "hello "+"world"
每個字符串都是由獨立編碼的Unicode字符組成白魂,并且提供了擁有訪問這些字符在不同的Unicode表示的支持。
Swfit中氏捞,String類型與
Foundation NSString
類進行了無縫橋接碧聪。如果需要利用Cocoa
或Cocoa Touch
中的Foundation
框架實現(xiàn)功能肠缨,整個NSString API
都可以調用創(chuàng)建的任意String類型的值碰煌。也可以在任意要求傳入NSString的實力作為參考的API中使用String類型作為替換箱玷。-
定義
let hello = "hello world"
let hello:String
// 也可以這樣拼接 注意前面是var
var hello02 = "hello,"
hello02.append("world")
// 打印字符個數(shù)
print(hello02.characters.count)
-
截取
// 截取一個字符
print(hello01[hello01.startIndex])
// 截取一個字符
print(hello01[hello01.index(before: hello01.index(hello01.endIndex, offsetBy:-1))])
// 截取一段字符
print(hello02[hello02.index(hello02.startIndex, offsetBy: 3)...hello02.index(hello02.endIndex, offsetBy: -1)])
-
插入
hello02.insert("!", at: hello02.endIndex)
hello02.insert(contentsOf: "hai".characters, at: hello02.startIndex)
-
刪除
hello02.remove(at: hello02.startIndex)
hello02.removeAll()
-
判斷
// 字符串相等
if hello01 == hello02 {
print("兩字符串相等")
}
// 判斷前綴
if hello01.hasPrefix("hel") {
print("有hel前綴")
}
// 判斷后綴
if hello01.hasSuffix("lo") {
print("有ol后綴")
}
// 判斷是否為空
if hello02.isEmpty {
print("字符串為空")
}
Array
- Swfit中窿克,數(shù)組是一種數(shù)據(jù)結構汗侵,用來存放多個類型相同的數(shù)據(jù)降铸,數(shù)據(jù)在數(shù)組中是有序的河胎。
- Swfit中勃刨,數(shù)組存放任意類型數(shù)據(jù)需要定義為
[Any]
- Swift中栋烤,數(shù)組依然用[]表示谒养。
-
定義
let arr01 = ["asd","fgh","jkl","zxc"]
let arr02:[String] = ["123","456","789","0"]
let arr03:[Any] = ["let",1,2,UIView()]
let arr04 = [String]()
let arr05:NSArray = ["123","456","789"]
let arr06:NSMutableArray = ["1","2","3","4"]
arr06.contains(3) //數(shù)組中是否存在'3' 結果為:true
-
添加、插入
var arr07 = ["hello","world","!"]
// 追加
arr07.append("test")
// 插入
arr07.insert("test two", at: 1)
#打印結果
["hello", "test two", "world", "!", "test"]
-
刪除
var arr08 = ["1","2","3"]
// 刪除某一項
arr08.remove(at: 0)
// 全部刪除
arr08.removeAll()
-
修改
var arr09 = ["4","5","6"]
arr09[0] = "7"
arr09[0...1] = ["8","9"]
print(arr09)
#打印結果
["8", "9", "6"]
-
遍歷
let arr10 = ["1","2","3","4"]
for str in arr10 {
print(str)
}
for (index,value) in arr10.enumerated() {
print("Index: \(index),Valuie: \(value)")
}
#打印結果
1
2
3
4
Index: 0,Valuie: 1
Index: 1,Valuie: 2
Index: 2,Valuie: 3
Index: 3,Valuie: 4
Dictionary
- 字典是集合類型存放多個鍵值對明郭,其中鍵是唯一的买窟,不能重復。
- 字典中存放的鍵值對是無序的薯定,寫入的順序和讀取的順序可能不同始绍。
- 字典中存放的數(shù)據(jù)是任意類型。
- Swift中话侄,字典和數(shù)組都用'[]'表示亏推。
-
定義
let dict01:[String:Int] = ["A":90,"B":80,"C":70]
let dict02 = [String:String]()
let dict03 = ["A":"10","B":"20","C":"30"]
-
添加、修改
swift中字典的增加和修改:如果有key就是修改年堆,如果沒有key吞杭,就是增加
var dict04 = ["name":"Lucy","sex":"woman"]
dict04["age"] = "18"
dict04["name"] = "Lili"
-
刪除
var dict05 = ["name":"Lucy","sex":"woman","age":"18"]
dict05.removeValue(forKey: "name")
dict05.removeAll()
-
遍歷
var dict06 = ["name":"Lucy","sex":"woman","age":"18"]
// 檢查是否為空
if dict06.isEmpty {
print("字典為空")
}
for (key,value) in dict06 {
print("Key:\(key),Value:\(value)")
}
for key in dict06.keys {
print("Key:\(key)")
}
for value in dict06.values {
print("Value:\(value)")
}
#打印結果
Key:name,Value:Lucy
Key:sex,Value:woman
Key:age,Value:18
Key:name
Key:sex
Key:age
Value:Lucy
Value:woman
Value:18
Set
- Swift中,集合
Set
可以存放多個相同類型數(shù)據(jù)变丧。 - Set存放的數(shù)據(jù)時無序的芽狗。
- Set中存放的數(shù)據(jù)不能重復,如果重復痒蓬,系統(tǒng)會自動刪除童擎。
let set01:Set = [1,2,3,4,5,6,7,8,8]
print(set01)
#打印結果
[2, 4, 5, 6, 7, 3, 1, 8]
- 插入曼月、刪除、遍歷
var set02:Set = [1,2,3,4,5,6]
set02.insert(7)
print(set02)
set02.remove(1)
print(set02)
set02.removeFirst()
print(set02)
for i in set02.sorted() {
print(i)
}
#打印結果
[2, 4, 5, 6, 7, 3, 1]
[2, 4, 5, 6, 7, 3]
[4, 5, 6, 7, 3]
3
4
5
6
7
End
頭好暈~~~