Swift-2-基本操作符

// Playground - noun: a place where people can play

import UIKit

// 基本运算符
// 运算符有3种: 单目运算符(如 -a),二目运算符(如a+b)和三目运算符(a ? b : c)

let (x, y) = (20, 30) // 分解元组
println("x is \(x), y is \(y)")
// 注意: 与C/OC不同的是,swift中=无返回值

// 求余运算 a % b  a = (b * 整数) + remainder  b 的符号将被忽略
9 % -2 // 1
// 浮点求余
9 % 3.3 // 2.4

//  nil coalescing operator
var a : Int? = 3
let b : Int = 2
var c = a ?? b // 等同于 c != nil ? c! : b 注意,使用时保证b与a的存储的类型一致。也就是说,a是Int?类型,b必须是Int类型
println("\(c)")

// 闭合区间 ...
for i in 0 ... 5 {
    println("\(i)") // 执行6次
}

// 半开半闭区间 ..<
for j in 0 ..< 5 {
    println("\(j)") // 执行5次
}

 

posted @ 2014-11-23 18:21  2020_xx  阅读(133)  评论(0编辑  收藏  举报