iOS swift变量,运算符

let str = "Hello, playground"
let btn = UIButton(frame: CGRect(x: 20, y: 30, width: 100, height: 100))
btn.backgroundColor = UIColor.red


let label = UILabel(frame: CGRect(x: 10, y: 20, width: 100, height: 50))
label.text = "Hello"
label.textAlignment = NSTextAlignment.center
label.font = UIFont.systemFont(ofSize: 25)
label.textColor = UIColor.blue

btn.addSubview(label)


//变量 声明和赋值 let:常量 var:变量,尽量用let 执行效率高 不能用let的才用var

//显式声明和隐式声明
let b = true

let myB1,myB2 :Bool
//隐式声明必须指定类型
let myInta:Int
myInta = 20

//等号两边必须对称
let x = 10
let y=20

//运算符自增自减变成+=1或-=1
x+=1
x-=1
//只能显式类型转换
let f = 10.2
let result = f/Double(x)
//逻辑判断语句 没有非0及真的概念少了括号
if x>y {
    print("x>y")
}
//Switch case少了break
switch x {
case 1:
    break
    
case 2:
    break
case 3:
    break
default:
    print("default")
    break
}

str = "a"
switch str {
case "a":
    print("a")
    break
case "ms":
    print("ms")
    break
default:
    break
    
}

//区间运算符
x = 40
switch x {
case 10...20:
    print("10-20")
    break
case 30..<40:
    print("30-40")
    break
default:
    print("default")
    break
}

//三目问号和条件必须有空格
let n = x>30 ?5:10

//for循环
for i in 0..<10 {
    print("index = \(i)")
}
//while循环
x=80
while x<100 {
    print("第\(x)次\(x)")
    x+=1
}
//相当于oc do~while
repeat{
    x-=2
    print("x=\(x)")
}while x>50

推荐阅读更多精彩内容