Swift语法(一)
Swift语法
1.前言
之前学过siwft语法,但是没有整理,这边简易总结简单语法。
2.HelloWorld
print("Hello World")
3.常量与变量
- 常量
swift支持让let作为常量,并且支持类型推断。
let PI = 3.1415
let PI2: Double = 3.1415
print(PI)
- 变量
swift支持使用var进行申明变量,并且变量支持Unicode编码
var width = 150
var width2: Int = 150
var π = 3.1415
var 🤠 = "laugh"
var 中文 = "中文"
4.类型与特征
数据类型
- Int 整型
- UInt 无符号整型
- Float、Double 浮点
- Bool 布尔
- String 字符串
- Character 字符
- Optional 可选类型
- Tuple 元祖
数据特征
- 别名
typealias Feet = Int
- 类型推断
let age = 40
let a: Int = 1
let b: UInt = 2
let c: Float = 1.1
let d: Double = 0.11
let e: Bool = true
let f: String = "Hello"
let g: Character = "A" // 注意Character需要使用双引号
let (on, tw): (Int, Int) = (1, 2)
print(on, tw)
let i: (String, Int) = ("hi", 2)
let hi = i.0
let two = i.1
print(hi, two)
// 可选类型
let address: String?
let age: Int?
5.基础操作符
几乎和c一致
var a = 6
var b = -a
var c = a + b
var d = a / b
var e = 5 % 2
var f = a >= b
var g = a <= b
var h = a == b
6.集合类型
数组
var arr = [1, 2, 3]
var arr2 = [Int](repeating: 3, count: 10)
var arr3 = Array(1...20)
var places = ["北京", "上海"]
- 计数
//计数
print(arr.count)
- 添加元素
//添加元素
arr.append(4)
arr += [5, 6, 7]
- 插入
//插入
places.insert("Paries", at: 1)
- 删除
//删除元素
places.remove(at: 1)
print(places)
- 查询
// 查询
print(places[0...1])
print(places[0])
集合
- 增加
var cardno: Set = [1, 2, 3]
cardno.insert(1)
cardno.insert(4)
print(cardno)
- 删除
cardno.remove(3)
print(cardno)
- 检查
cardno.contains(1)
- 转换数组
let arr: Array<Int> = cardno.sorted()
print(type(of: arr))
- 交叉并补
var arr: Set<Int> = [1, 2, 3]
var arr2: Set<Int> = [2, 3, 4]
var arr3: Set<Int> = arr.intersection(arr2)
var arr4: Set<Int> = arr.subtracting(arr2)
var arr5: Set<Int> = arr.union(arr2)
var arr6: Set<Int> = arr.symmetricDifference(arr2)
字典
- 字典的取值
var dic = ["apple": 1.1, "banana": 1]
print(dic["apple"]!) // 取值不存在返回nil
- 添加更新
dic["pear"] = 1
- count
print(dic.count)
7.循环控制
- for in循环
var arr = [1, 2, 3, 4, 5]
for ele in arr {
print(ele)
}
for ele in 1...100 {
print(ele)
}
for _ in 1...10 {
print("learn swift")
}
- while循环
var i = 0
while i != 100 {
i += 2
}
- continue and break
var i = 0
while true {
if i == 50 {
break
}
if i == 2 {
continue
}
print("steve yu")
i += 1
}
for ele in 1...50 {
if ele == 10 {
continue
}
print("haha")
}
- switch
var choice = 1
switch choice {
case 1:
print("hahah")
case 10...30:
print("20 to 30")
default:
print("no")
}
8.函数
_是外部变量,如果不加,贼要写变量名
func add(_ x: Int, _ y: Int) -> Int {
return x + y
}
func prin() {
print("haha")
}
prin()
add(1, 2)
func minus(c x:Int, d y: Int) -> Int {
return x - y
}
minus(c: 1, d: 2)
func multi(x:Int, y: Int) -> Int {
return x * y
}
multi(x: 9, y: 2)
函数参数默认值
func incr(_ num: Int, _ incrNum: Int = 10) -> Int {
return num + incrNum
}
incr(5)
函数传参
func handle(_ x: Int, _ y: Int, _ method: (Int, Int)->Int) -> Int {
return method(1, 2)
}
handle(1, 2, add)
闭包
var city = ["shanghai", "hangzhou", "beijing"]
//sorted函数
var cityRank1 = city.sorted()
//函数调用改写
func cmp(a: String, b: String) -> Bool {
return a < b
}
var cityRank2 = city.sorted(by: cmp)
//闭包改写
var cityRank3 = city.sorted{(a, b) -> Bool in
return a > b
}
//简写
var cityRank4 = city.sorted{(a, b) in a > b}
//快捷参数
var cityRank5 = city.sorted{$0 > $1}
9.枚举
enum Weather {
case sunny
case cloudy
case rainy
case snow
case froggy
}
var todayWeather: Weather = Weather.froggy
switch todayWeather {
case .sunny:
print("大晴天")
case .froggy:
print("大雾天")
case .rainy:
print("下雨天")
case .snow:
print("大雪天")
case .cloudy:
print("多云天")
}