Swift是编程语言的最新研究成果,并结合数十年的经验建设苹果平台的结果。结转自Objective-C的命名参数表示在一个干净的语法,使得Swift的API更容易阅读和维护
众所周知swift曾经一度同Kotlin被谷歌列为安卓开发语言的首选语言之一,由于某些原因最终选择Kotlin了,苹果在2014年WWDC首次推出并称在20年内让全球的每一台智能设备都用swift语言来开发。
Swift特有许多其他的功能,使你的代码更传神:
闭包的统一与函数指针
元组和多个返回值
泛型
快速而简洁的迭代范围或集合
支持的方法,扩展的协议结构。
函数式编程模式,例如:映射(map)和过滤器(filter)
目前更新至swift 3.0 部分之前的 C 方法在3.0里已经不再支持 可升级最新版Xcode为8.3.2版本
代表示例:
swift 3.0没有C类型的for循环了,用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 变量:类型?,默认是无值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 字符串插值:把【常量/变量/字面量/表达式】组合成一个长字符串
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 数组
//1⃣️创建一个有默认值的数组
let array : [Int]
array = [Int](repeatElement(3, count: 10))//10个元素3
print(array)
//输出的:[3, 3, 3, 3, 3, 3, 3, 3, 3, 3]
//2⃣️创建一个有序范围的Int数组,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⃣️用数组字面量来创建数组:[值1,值1,值1,...,值N]
var places = ["beijing","shanghai","guangzhou","shenzhen"]
print(places)
//输出
/*
["beijing", "shanghai", "guangzhou", "shenzhen"]
*/
//4⃣️.1元素计数:count,空否:isEmpty
let count_arr = places.count
let empty = places.isEmpty
print(count_arr,empty)
//输出4 false
//4⃣️.2 合并
//数组追加元素
places.append("dezhou")//追加一个元素
print(places)//输出["beijing", "shanghai", "guangzhou", "shenzhen", "dezhou"]
//数组追加数据合并为一个数组
let haiwaiPlaces = ["NewYork","london","Sao paolu"]
places += haiwaiPlaces
print(places)
//输出
/*
["beijing", "shanghai", "guangzhou", "shenzhen", "dezhou", "NewYork", "london", "Sao paolu"]
*/
//4⃣️.3 获取元素:数组名[索引],⚠️索引总是从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)
/*
输出结果 少了索引为3的 nanjing 输出成功
["beijing", "shanghai", "guangzhou", "shenzhen", "dezhou", "NewYork", "london", "Sao paolu"]
*/
13 集合<Set> 值无序不重复,适合存唯一性的数据 ,如用户名/身份证号/护照号等
//1⃣️
let cardno : Set = [1,2,3,4,5,1,2,3,4]
print(cardno)
//输出[2, 4, 5, 3, 1]
//2⃣️用数组字面量创建集合
var citys : Set = ["shanghai","beijing","dezhou","shanghai","beijing","dezhou"]
//2⃣️.1元素计数:count ,空否 :isEmpty
print("元素\(citys.count)个,是否为空:\(citys.isEmpty)")
//输出: 元素3个,是否为空:false 去除重复元素只计数 非重复的元素的个数
//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 转换为数组: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⃣️ 判断个数 是否为空
print("元素\(dic.count)个数,是否为空\(dic.isEmpty)")//元素3个数,是否为空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⃣️移除,用下表把值设为nil
dic["shengao"] = nil
print(dic)
/*
输出 去除了身高的那一项
["name": "jack", "age": "12", "birsday": "1993-09-04"]
*/
//5⃣️循环一个字典 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⃣️把键值对分离成数组,用[数组类型](字典变量.keys),[数组类型](字典变量.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只需循环固定次数,可以省略项名,比如打印10次相同文本
for _ in 1...10 {
print("学习swift")
}
16 .while循环执行一系列操作 直到条件不成立 使用场合:执行次数未知
var i = 1
var sum = 0
while i<=100 {
sum = sum + i
i += 1
}
print(i,sum)
//当i = 101 时跳出循环
//101 5050
//16.1
var j = 1
while j<10 {
print("while循环示例")
j += 1
}
//输出是打印了十遍while循环示例
//17 判断
var temp = 23
if temp > 35 {
print("开空调")
} else {
print("不用开空调")
}
17.1 continue 结束本次循环 继续循环,break跳出整个循环到此为止
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循环了,用for in吧
- 早退语句 :在必需的条件下不满足的早期阶段推出执行。语句关键字guard,与if相似。属于条件强调意味很浓的语句。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("可以乘坐本次车")
}
//调用
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范围内")
default:
print("\(xy)不在星球上")
break
}
//输出 (0, 2)在Y轴上
20 函数 (参数)
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定义多个参数的函数 用元组
func yuanzuhanshu(a:Int,b:Int,c:Int) -> (Int){
return a + b + c
}
print(yuanzuhanshu(a: 1, b: 1, c: 1))//3
20.2可以给某个参数设置默认值
func add2(a:Int,b:Int,c:Int = 20) -> Int {
return a+b+c
}
print(add2(a: 1, b: 2))//23
20.3函数类型 包含参数和返回类型的简写形式,可以像普通变量那样使用,一般用于函数式编程
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 闭包 没有名称的函数 把参数和返回值放入花括号里
//sorted(),用于对数组进行排序,只接受一个函数类型的参数,描述排序逻辑
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)
//用闭包表达式进行改写
var cityArray = array.sorted { (a, b) -> Bool in
return a>b
}
print(cityArray)
//输出["D", "C", "B", "A"]
//这就是闭包写法
21.2闭包的自动推断
//参数和“返回类型”可自动推断,单表达式可以忽略“return”关键词
var cityArray2 = array.sorted { (a, b) in
a>b
}
print(cityArray2)
//使用快捷参数,前缀$,从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, "湛蓝")
let shanghaiwumai = tianqi.mai("PM2.5", 100)
switch shanghaiwumai {
case .qing(let ziwai,let liangshai,let dec):
print("紫外指数:",ziwai,"晾晒:",liangshai,"天颜色:",dec)
case .mai(let leibie,let index):
print("类别:",leibie,"指数",index)
}
switch shanghaiTianQi {
case .qing(let ziwai,let liangshai,let dec):
print("紫外指数:",ziwai,"晾晒:",liangshai,"天颜色:",dec)
case .mai(let leibie,let index):
print("类别:",leibie,"指数",index)
}
/*输出
类别: PM2.5 指数 100
紫外指数: 30 晾晒: 50 天颜色: 湛蓝
*/
No End 待续...