Swift是編程語言的最新研究成果,并結(jié)合數(shù)十年的經(jīng)驗建設(shè)蘋果平臺的結(jié)果矛绘。結(jié)轉(zhuǎn)自O(shè)bjective-C的命名參數(shù)表示在一個干凈的語法甸鸟,使得Swift的API更容易閱讀和維護(hù)
眾所周知swift曾經(jīng)一度同Kotlin被谷歌列為安卓開發(fā)語言的首選語言之一,由于某些原因最終選擇Kotlin了哮塞,蘋果在2014年WWDC首次推出并稱在20年內(nèi)讓全球的每一臺智能設(shè)備都用swift語言來開發(fā)纷闺。
Swift特有許多其他的功能算凿,使你的代碼更傳神:
閉包的統(tǒng)一與函數(shù)指針
元組和多個返回值
泛型
快速而簡潔的迭代范圍或集合
支持的方法,擴展的協(xié)議結(jié)構(gòu)犁功。
函數(shù)式編程模式氓轰,例如:映射(map)和過濾器(filter)
目前更新至swift 3.0 部分之前的 C 方法在3.0里已經(jīng)不再支持 可升級最新版Xcode為8.3.2版本
代表示例:
swift 3.0沒有C類型的for循環(huán)了,用for in吧
開始
1.var 定義變量
var str = "heal"
2.let 定義常量
let num = 10
let MI_P = 3.1415926
print(MI_P)
var current = 20
print(num,current,str)
3.int
var runtoday = 5
runtoday = 6
print(runtoday)
4.double
var doubleshu:Double = 0.5
//doubleshu = 3 把這個值改成原始類型的值 為的是安全起見 防止意外的錯誤
//類型安全
print(doubleshu)
5.Bool
var isok = true
isok = false
print(isok)
6.可選類型(Optional) Optional定義的量可有值 可無值
如用戶選填部分
形式:var 變量:類型浸卦?署鸡,默認(rèn)是無值nil
var addr : String?
addr = "woshidouxindong"
print(addr ?? 0)
var num : intmax_t?
num = 1
print(num ?? 0)
7.判斷是不是空
var a = ""
print(a.isEmpty)//是空 true
var b = " "
print(b.isEmpty)//不是空 因為有空格 代表著某種字符
8.字符Character是單個字符的意思
var a: Character = "我"
var b: Character = "你"
print(a)
print(b)
9.把words這個句子單個拆開
let words = "hellowere"
//words.characters 是把words這個句子單個拆開
for word in words.characters {
//逐個輸出
print(word)
}
10.字符串拼接
let a = "你"
let b = "好"
let c = "嗎"
var famous = a + b + c
print(famous)//輸出:你好嗎
let str1: Character = "安"
let str2: Character = "迪"
famous.append(str1)
famous.append(str2)
print(famous)//輸出:你好嗎安迪
11 字符串插值:把【常量/變量/字面量/表達(dá)式】組合成一個長字符串
let name = "xiaoming"
let type = "G"
let number = 11
let price = 100.8
let message = "\(name)定了\(type)類型的\(number)票,支付\(price * 3)元"
print(message)
//輸出xiaoming定了G類型的11票,支付302.4元
11.1特殊字符
\0 \ \t \n \r " ' \u{n}
let strings = "xiao\0ming\t定了\nG類型\r的11票,支付302.4\\u{n}元"
print(strings)
12 數(shù)組
//1??創(chuàng)建一個有默認(rèn)值的數(shù)組
let array : [Int]
array = [Int](repeatElement(3, count: 10))//10個元素3
print(array)
//輸出的:[3, 3, 3, 3, 3, 3, 3, 3, 3, 3]
//2??創(chuàng)建一個有序范圍的Int數(shù)組,Array(起始值...終止值)
let array2 = Array(0...100)
print(array2)
//輸出
/*
[0, 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, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100]
*/
//3??用數(shù)組字面量來創(chuàng)建數(shù)組:[值1,值1,值1,...,值N]
var places = ["beijing","shanghai","guangzhou","shenzhen"]
print(places)
//輸出
/*
["beijing", "shanghai", "guangzhou", "shenzhen"]
*/
//4??.1元素計數(shù):count限嫌,空否:isEmpty
let count_arr = places.count
let empty = places.isEmpty
print(count_arr,empty)
//輸出4 false
//4??.2 合并
//數(shù)組追加元素
places.append("dezhou")//追加一個元素
print(places)//輸出["beijing", "shanghai", "guangzhou", "shenzhen", "dezhou"]
//數(shù)組追加數(shù)據(jù)合并為一個數(shù)組
let haiwaiPlaces = ["NewYork","london","Sao paolu"]
places += haiwaiPlaces
print(places)
//輸出
/*
["beijing", "shanghai", "guangzhou", "shenzhen", "dezhou", "NewYork", "london", "Sao paolu"]
*/
//4??.3 獲取元素:數(shù)組名[索引]靴庆,??索引總是從0開始的 別越界
let yuansu = places[3]
print("索引為3的值\(yuansu)")//輸出:索引為3的值shenzhen
//4??.4 插入:insert
places.insert("nanjing", at: 3)
print(places)
//輸出
/*
把南京插入到索引為3的位置
["beijing", "shanghai", "guangzhou", "nanjing", "shenzhen", "dezhou", "NewYork", "london", "Sao paolu"]
*/
//4??.5移除
places.remove(at: 3)
print(places)
/*
輸出結(jié)果 少了索引為3的 nanjing 輸出成功
["beijing", "shanghai", "guangzhou", "shenzhen", "dezhou", "NewYork", "london", "Sao paolu"]
*/
13 集合<Set> 值無序不重復(fù),適合存唯一性的數(shù)據(jù) 怒医,如用戶名/身份證號/護(hù)照號等
//1??
let cardno : Set = [1,2,3,4,5,1,2,3,4]
print(cardno)
//輸出[2, 4, 5, 3, 1]
//2??用數(shù)組字面量創(chuàng)建集合
var citys : Set = ["shanghai","beijing","dezhou","shanghai","beijing","dezhou"]
//2??.1元素計數(shù):count 炉抒,空否 :isEmpty
print("元素\(citys.count)個,是否為空:\(citys.isEmpty)")
//輸出: 元素3個稚叹,是否為空:false 去除重復(fù)元素只計數(shù) 非重復(fù)的元素的個數(shù)
//2??.2插入: insert
citys.insert("nanjing")
print(citys)
//輸出["shanghai", "dezhou", "nanjing", "beijing"]
//2??.3移除
citys.remove("shanghai")
print(citys)//["dezhou", "nanjing", "beijing"]
//2??.4是否包含元素:contains
print(citys.contains("dezhou"))//輸出 true
//2??.5 轉(zhuǎn)換為數(shù)組:sorted
let citysArray = citys.sorted()
print(citysArray)//["beijing", "dezhou", "nanjing"]
//3??.1 交集 找出兩個集合相同的元素
let srray = ["nanjing"]
let arys = citys.intersection(srray)
print(arys)//輸出["nanjing"]
//3??.2 差集
let sub = citys.subtracting(arys)
print(sub)//輸出["dezhou", "beijing"]
//3??.3并集
let sum = citys.union(sub)
print(sum)//輸出["dezhou", "nanjing", "beijing"]
14 字典 鍵值對
//1??
var dic = ["name":"jack","age":"12","birsday":"1993-09-04"]
print(dic)
//2?? 判斷個數(shù) 是否為空
print("元素\(dic.count)個數(shù),是否為空\(dic.isEmpty)")//元素3個數(shù),是否為空false
//3??添加或更新元素
dic["shengao"] = "176"
print("添加身高元素之后的dic ==\(dic)")
//輸出
/*
添加身高元素之后的dic ==["name": "jack", "age": "12", "birsday": "1993-09-04", "shengao": "176"]
*/
dic["shengao"] = "178"
print("更新身高元素之后的dic ==\(dic)")
//輸出
/*
更新身高元素之后的dic ==["name": "jack", "age": "12", "birsday": "1993-09-04", "shengao": "178"]
*/
//獲取某項的情況
let shengao : String = dic["shengao"]!
print("身高是\(shengao)")//身高是178
//4??移除焰薄,用下表把值設(shè)為nil
dic["shengao"] = nil
print(dic)
/*
輸出 去除了身高的那一項
["name": "jack", "age": "12", "birsday": "1993-09-04"]
*/
//5??循環(huán)一個字典 for in ,因為鍵值對有2個元素,用元組變量
for (key ,value) in dic {
print(key,value)
}
//輸出
/*
name jack
age 12
birsday 1993-09-04
*/
//6??單獨使用 鍵值對里面的keys 或者 values(可使用for in)
for keys in dic.keys{
print(keys)
}
//輸出
/*
name
age
birsday
*/
for values in dic.values{
print(values)
}
//輸出
/*
jack
12
1993-09-04
*/
//7??把鍵值對分離成數(shù)組扒袖,用[數(shù)組類型](字典變量.keys),[數(shù)組類型](字典變量.values)
let mingcheng = [String](dic.keys)
let qingkuang = [String](dic.values)
print(mingcheng,qingkuang)
/*
輸出
["name", "age", "birsday"]
["jack", "12", "1993-09-04"]
*/
15 .控制流 for - in
//15.1
let places = ["beijing","guangzhou","shenzhen"]
for place in places {
print(place)
}
/*輸出
beijing
guangzhou
shenzhen
*/
var sum = 0
for i in 1...100 {
sum += i
}
print(sum)
//5050
//15.2只需循環(huán)固定次數(shù)塞茅,可以省略項名,比如打印10次相同文本
for _ in 1...10 {
print("學(xué)習(xí)swift")
}
16 .while循環(huán)執(zhí)行一系列操作 直到條件不成立 使用場合:執(zhí)行次數(shù)未知
var i = 1
var sum = 0
while i<=100 {
sum = sum + i
i += 1
}
print(i,sum)
//當(dāng)i = 101 時跳出循環(huán)
//101 5050
//16.1
var j = 1
while j<10 {
print("while循環(huán)示例")
j += 1
}
//輸出是打印了十遍while循環(huán)示例
//17 判斷
var temp = 23
if temp > 35 {
print("開空調(diào)")
} else {
print("不用開空調(diào)")
}
17.1 continue 結(jié)束本次循環(huán) 繼續(xù)循環(huán)僚稿,break跳出整個循環(huán)到此為止
for i in 1...10 {
if i == 5 {
break
// continue
}
print(i,"哈哈哈哈??")
}
/*
break
1 哈哈哈哈??
2 哈哈哈哈??
3 哈哈哈哈??
4 哈哈哈哈??
continue
1 哈哈哈哈??
2 哈哈哈哈??
3 哈哈哈哈??
4 哈哈哈哈??
6 哈哈哈哈??
7 哈哈哈哈??
8 哈哈哈哈??
9 哈哈哈哈??
10 哈哈哈哈??
*/
//注意??swift 3.0沒有C類型的for循環(huán)了,用for in吧
- 早退語句 :在必需的條件下不滿足的早期階段推出執(zhí)行蟀伸。語句關(guān)鍵字guard蚀同,與if相似缅刽。屬于條件強調(diào)意味很濃的語句。guard let 與 if let 相似蠢络,用于可選擇類型衰猛。guard 有助于提高代碼的可讀性。
var haveMoney = true
var price : Int? = 3
func chengChe() {
guard haveMoney else {
print("沒錢不可以乘坐本次車")
return
}
guard let myMoney = price,myMoney >= 3 else {
print("錢不夠刹孔,不可以乘坐本次車")
return
}
print("可以乘坐本次車")
}
//調(diào)用
chengChe()//可以乘坐本次車
- switch 使用場景:多于兩個條件的
var temp = 30
switch temp {
case 25...34:
print("室外25-34度")
case 35...40:
print("室外35-40度")
default:
print("室外超過40度")
}
//輸出:室外25-34度
//19.1 元組匹配 位置
let xy = (0,2)
switch xy {
case (0,0):
print("\(xy)在原點")
case (_,0):
print("\(xy)在X軸上")
case(0,_):
print("\(xy)在Y軸上")
case(-2...2,-2...2):
print("\(xy)在2*2范圍內(nèi)")
default:
print("\(xy)不在星球上")
break
}
//輸出 (0, 2)在Y軸上
20 函數(shù) (參數(shù))
func add(a:Int,b:Int) -> (Int){
return a+b
}
let sum = add(a: 2, b: 5)
print(sum)
//輸出7
func chengche(price :Int) -> String {
return "keyi"
}
let keyi : String? = chengche(price: 10)
print(keyi ?? 0)//keyi
20.1定義多個參數(shù)的函數(shù) 用元組
func yuanzuhanshu(a:Int,b:Int,c:Int) -> (Int){
return a + b + c
}
print(yuanzuhanshu(a: 1, b: 1, c: 1))//3
20.2可以給某個參數(shù)設(shè)置默認(rèn)值
func add2(a:Int,b:Int,c:Int = 20) -> Int {
return a+b+c
}
print(add2(a: 1, b: 2))//23
20.3函數(shù)類型 包含參數(shù)和返回類型的簡寫形式啡省,可以像普通變量那樣使用,一般用于函數(shù)式編程
func create(a:Int,b:Int,action:(Int,Int)->Int)-> Int{
return action(a,b)
}
func adds(a:Int,b:Int) -> Int {
return a+b
}
func jian(a:Int,b:Int) -> Int {
return a-b
}
func cheng(a:Int,b:Int) -> Int {
return a*b
}
func chu(a:Int,b:Int) -> Int {
return a/b
}
let sumq = create(a: 10, b: 5, action: adds)
let subq = create(a: 10, b: 5, action: jian)
let chengq = create(a: 10, b: 5, action: cheng)
let chuq = create(a: 10, b: 5, action: chu)
print(sumq,subq,chengq,chuq)
//輸出:15 5 50 2
21 閉包 沒有名稱的函數(shù) 把參數(shù)和返回值放入花括號里
//sorted()髓霞,用于對數(shù)組進(jìn)行排序卦睹,只接受一個函數(shù)類型的參數(shù),描述排序邏輯
var array = ["D","C","A","B"]
var newArray = array.sorted()
print(newArray)//輸出["A", "B", "C", "D"]
//21 倒序 輸出
func daoxu(a:String,b:String)-> Bool{
return a > b
}
var hahArray = array.sorted(by: daoxu)
print(hahArray)
//用閉包表達(dá)式進(jìn)行改寫
var cityArray = array.sorted { (a, b) -> Bool in
return a>b
}
print(cityArray)
//輸出["D", "C", "B", "A"]
//這就是閉包寫法
21.2閉包的自動推斷
//參數(shù)和“返回類型”可自動推斷方库,單表達(dá)式可以忽略“return”關(guān)鍵詞
var cityArray2 = array.sorted { (a, b) in
a>b
}
print(cityArray2)
//使用快捷參數(shù)结序,前綴$,從0開始遞增
var cityArray3 = array.sorted{
$0 > $1
}
print("\(cityArray3)")
/*
輸出
["D", "C", "B", "A"]
*/
*/
22 枚舉
enum Weather {
case sunny
case cloudy
case rainy
case snow
case froggy
}
print(Weather.sunny)
var todayWeather = Weather.sunny
switch todayWeather {
case .cloudy:
print("今天天氣多云")
case .sunny:
print("今天天氣晴朗")
case .rainy:
print("今天天氣下雨")
default:
break
}
//輸出今天天氣晴朗
22.2附加值 :每一種類型都可以附加一個或多個值纵潦,形式是元組 形式 case(Int,String,...)
enum tianqi {
case qing(Int,Int,String)
case mai(String,Int)
}
//附上附加值:加上元組字面量
let shanghaiTianQi = tianqi.qing(30, 50, "湛藍(lán)")
let shanghaiwumai = tianqi.mai("PM2.5", 100)
switch shanghaiwumai {
case .qing(let ziwai,let liangshai,let dec):
print("紫外指數(shù):",ziwai,"晾曬:",liangshai,"天顏色:",dec)
case .mai(let leibie,let index):
print("類別:",leibie,"指數(shù)",index)
}
switch shanghaiTianQi {
case .qing(let ziwai,let liangshai,let dec):
print("紫外指數(shù):",ziwai,"晾曬:",liangshai,"天顏色:",dec)
case .mai(let leibie,let index):
print("類別:",leibie,"指數(shù)",index)
}
/*輸出
類別: PM2.5 指數(shù) 100
紫外指數(shù): 30 晾曬: 50 天顏色: 湛藍(lán)
*/
No End 待續(xù)...