标签:swift
import Foundation
//***********************************************************************************************
//1.Basic Operators(基本操作符)
//_______________________________________________________________________________________________
//简介
//操作符是一种特殊的符号或短语用来检查,改变,或组合值。
//***********************************************************************************************
//2.Assignment Operator(赋值运算符)
//_______________________________________________________________________________________________
//简单的赋值
let b =10
var a =5
a =b //将 b的值赋予 a
println("the a is\(a)")
//_______________________________________________________________________________________________
//元组赋值(如果分配的右侧是一个元组与多个值,它的元素可以分解成多个常量或变量)
let (x, y) = (1,2)
/*
if x = y{ //这段代码无法运行,因为 x= y 是一句赋值语句,而不是 x == y 这样的判断语句,因为 x, y都是 let 定义的常量,所以不能赋新的值,所以会报错
println("x is equal to y")
}
*/
//_______________________________________________________________________________________________
//取余运算符
let remainderNumber1 =9 % 4
let remainderNumber2 =8 % 2.3 //Swift 与 C/Objective-C 不同的是 Swift 支持浮点数取余运算
println("remainderNumber1 is\(remainderNumber1),remainderNumber2 is\(remainderNumber2)")
//_______________________________________________________________________________________________
//自增自减运算符
var aNumber =0
let bNumber = ++aNumber //先增加 aNumber 的值,此时 aNumber 从 0 增加为 1 然后赋值给 bNumber
let cNumber = aNumber++ //先把 aNumber 的为 1 的值赋值给 cNumber,然后 aNumber从 1 增加到 2
println("the aNumber is\(aNumber), the bNumber is\(bNumber), the cNumber is\(cNumber)")
//_______________________________________________________________________________________________
//一元减号运算符
let three = -3
let minusThree = -three //一元减号运算符改变变量的正负
println("the minusThree is\(minusThree)")
//***********************************************************************************************
//4.Compound Assignment Operators(复合赋值运算符)
//_______________________________________________________________________________________________
//复合赋值运算符使用方法
var aNumber2 =1
aNumber2 += 2 //这句话等同于 aNumber = aNubmer + 2
println("aNumber2 is\(aNumber2)")
//***********************************************************************************************
//5.Comparison Operators(对比运算符)
//_______________________________________________________________________________________________
//对比运算符应用
let aNumber3 =1
let bNumber3 =2
ifaNumber3 == bNumber3{ //两个值相等
println("two number is equal")
}
ifaNumber3 != bNumber3{ //两个值不相等
println("two number is unequal")
}
if aNumber3 <bNumber3{ //aNumber3 < bNumber3
println("aNumber3 < bNumber3")
}
if aNumber3 >bNumber3{ //aNumber3 > bNumber3
println("aNumber3 > bNumber3")
}
if aNumber3 <=bNumber3{ //aNumber3 <= bNumber3
println("aNumber3 <= bNumber3")
}
if aNumber3 >=bNumber3{ //aNumber3 >= bNumber3
println("aNumber3 >= bNumber3")
}
//***********************************************************************************************
//6.Ternary Conditional Operator(三目运算符)
//_______________________________________________________________________________________________
//使用三目运算符简化逻辑代码
let contentHeight =40
let hasHeader =true
let rowHeight = contentHeight + (hasHeader ?50 : 20) //使用 "判断? result1: result2" 来简化逻辑,先判断 hasHeder 的真假,如果为真,返回 50,如果为假,返回 20
println("the rowHeight is\(rowHeight)")
//_______________________________________________________________________________________________
//使用 if 语句对比三目运算符
let contentHeight1 =40
let hasHeader1 =true
var rowHeight1 =contentHeight1
if hasHeader1{
rowHeight1 = rowHeight1 + 50
}
else{
rowHeight1 = rowHeight1 + 20
}
println("the rowHeight1 is\(rowHeight1)")
//***********************************************************************************************
//7.Range Operators(范围运算符)
//_______________________________________________________________________________________________
//使用 "..."表示前闭后闭区间
for indexin 1...5{
println("\(index) times 5 is\(index * 5)")
}
//_______________________________________________________________________________________________
//使用 "..<"表示前闭后开区间
for indexin 1..<5{
println("\(index) times 6 is\(index * 6)")
}
//_______________________________________________________________________________________________
//使用传统方法实现上面代码
forvar index = 1; index <5; ++index{
println("\(index) times 7 is\(index * 7)")
}
//***********************************************************************************************
//7.Logical Operators(逻辑运算符)
//_______________________________________________________________________________________________
//逻辑运算符 "!"的使用(逻辑非)
let allowedEntry =false
if !allowedEntry{ //allowEntry 原来的值为 false,现在用逻辑非 "!"之后,if 语句返回 true,继续运行大括号中的代码
println("Access Denied")
}
//_______________________________________________________________________________________________
//逻辑运算符 "&&"的使用(逻辑与)
let enteredDoorCode =true
let passedRetinaScan =false
if enteredDoorCode &&passedRetinaScan{ //使用逻辑与运算符 "&&" ,当两个参数都为真的时候,返回的结果才为真
println("Welcome")
}
else{
println("Access Denied")
}
//_______________________________________________________________________________________________
//逻辑运算符 "||"的使用(逻辑或)
let hasDoorKey =false
let knowsOverridePassword =true
if hasDoorKey ||knowsOverridePassword{ //使用逻辑或运算符 "||" ,当两个参数有一个为真的时候,返回结果为真
println("welcome")
}
else{
println("Access Denied")
}
//_______________________________________________________________________________________________
//复合逻辑运算实例
if enteredDoorCode &&passedRetinaScan || hasDoorKey ||knowsOverridePassword{ //假 ||假 ||真=真
println("welcome")
}
else{
println("Access Defnied")
}
寒城攻略:Listo 教你 25 天学会 Swift 语言 - 04 Basic Operators,布布扣,bubuko.com
寒城攻略:Listo 教你 25 天学会 Swift 语言 - 04 Basic Operators
标签:swift
原文地址:http://blog.csdn.net/u013096857/article/details/37870433