美文网首页
Swift 基本运算

Swift 基本运算

作者: CaptainRoy | 来源:发表于2018-08-05 09:51 被阅读2次
元组(Tuples) 比较
var result = (1,"a") < (2,"b")
print(result) // true
Nil-Coalescing运算符
var defaultColor = "red"
var definedColor: String?  //默认为nil
var colorName = definedColor ?? defaultColor // definedColor 为 nil , 因此 colorName 为 defaultColor 的值
print(colorName) // red
范围运算
  • a <= x <= b
/*
 value of index is 1
 value of index is 2
 value of index is 3
 value of index is 4
 value of index is 5
 */
for index in 1...5 {
    print(" value of index is \(index) ")
}
let names = ["Roy","Alex","lily","Brain","Bob"]
let count = names.count
for index in 0..<count {
    print(" name : \(names[index]) ")
}
/*
 name : Roy
 name : Alex
 name : lily
 name : Brain
 name : Bob
 */
/*
 lily
 Brain
 Bob
 */
for name in names[2...] {
    print(name)
}
/*
 Roy
 Alex
 lily
 */
for name in names[...2] {
    print(name)
}

相关文章

网友评论

      本文标题:Swift 基本运算

      本文链接:https://www.haomeiwen.com/subject/fpavvftx.html