Swift教程之运算符

2015-07-10 11:42
import Foundation

//4、复合赋值操作符
var a = 1
a += 2

//一元减运算符(一个数值前加了符号-,叫作一元减运算符)
let three = 3
let minusThree = -three //minusThree = -3
let plusThree = -minusThree // plusThree = 3

//一元加运算符(一元加运算符(+)返回的值,不做任何变动)
let minusSix = 6
let alsoMinusSix = +minusSix // alsoMinusSix = 6

//6、三元条件运算符
//特别注意:三目运算符里面的(?)前面一定要有至少一个空格
let contentHeight = 40
let rowHeight = contentHeight + (contentHeight > 20 ? 50 : 20)

//7、范围运算符
//封闭范围运算符(包括a和b)
for index in 1...5 {
    print("\(index) times 5 is \(index * 5)")
}
//半封闭的区域运算符(包头不包尾)
let names = ["Anna", "Alex", "Brian", "Jack"]
let count = names.count
for i in 0..<count
{
    print("Person \(i) is called \(names[i])")
}

版权声明:本文为博主原创文章,未经博主允许不得转载。