swift的语法小demo5(for循环,元组)
import Foundation /* swift可以return一个元组,好像挺有用的,也就是说可以返回一个对象所有的键值对 */ let http404Error = (404,"not found") let (statusCode,statusMessage) = http404Error println("\(statusCode) \(statusMessage)") let (justTheStatusCode,_) = http404Error println("\(justTheStatusCode)") println("\(http404Error.0) \(http404Error.1)") let http200Status = (statusCode:200,description:"ok") println("\(http200Status.0) \(http200Status.1)") println("\(http200Status.statusCode) \(http200Status.description)") let possibleNumber = "123w" let convertedNumber = possibleNumber.toInt() if convertedNumber { println("\(convertedNumber!) ee") } //assert使用场景:当必须保证age小于0时,可以加上assert方法确保万无一失 let age = -3 assert(age <= 0, "A person's age cannot be less than zero") /* 循环:for两个点和三个点的区别 */ for index in 1...5{ println("\(index)") } for index in 1..5{ println("\(index)") }