前端之家收集整理的这篇文章主要介绍了
Swift教程之运算符,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
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])")
}