import UIKit
Swift是一门强类型语言
1,类型推导,所以定义并直接赋值的时候可以省略其类型
let a = 10
let b = 2.4
let view = UIView()
let btn = UIButton()
//var m;wrong
var x: Int
let m = 4;
let n = 7;
2,swift在运算的时候必须保证类型一致,否则会报错,因为swift中没有类型隐式转换
let i = 20
let j = 10.4
let resultDouble = Double(i) + j //这里的m必须转换成浮点型才能进行运算
let resultInt = i + Int(j)
3,逻辑分支
- guard用法:必须用在函数内部(return, break, continue, throw)
let price = 15;
//guard price<15 else {
// print("I can not afford it")
// return;
//}
func canBuy(price:Int){
guard price<30 else {
print("I can not buy it")
return
}
print("I can but it!")
}
canBuy(price: price)
if a>10{
print("zhang")
}else{
print("danfeng")
}
if a<4{
print("zhang")
}else if a>=4&&a<10{
print("dan")
}else if a>=10{
print("feng")
}
let max = m>n ? m:n