首页 > 代码库 > Swift-5-流程控制
Swift-5-流程控制
// Playground - noun: a place where people can playimport UIKit// For-In 循环// 1 遍历数字区间for index in 1...5 { println("\(index) times 5 is \(index)")}// 2 遍历数组let names = ["Anna", "Alex", "Brain", "Jack"]for name in names { println("hello, \(name)")}// 3 遍历字典let numberOfLegs = ["spider" : 8, "ant" : 6, "cat" : 4]for (name, legCount) in numberOfLegs { println("\(name) has \(legCount) legs")}// 4 遍历字符for character in "hello" { println(character)}// For 循环for var index = 0; index < 3; index++ { println("index is \(index)")}// While do-whilevar i = 0do { i++} while (i < 3)while (i < 5) { i++}// 条件语句 Conditional Statementsvar temperatureInFahrenheit = 30if temperatureInFahrenheit <= 32 { println("It‘s very cold")}switch temperatureInFahrenheit { case 1...20: println("1...20") case 23, 30: println("come here") fallthrough // 默认不会进入下一个判断,加上这个关键字可以让匹配直接进入下一个 default: println("default") break // 跳出switch}var somePoint = (1, 1)switch somePoint { case let (x, y) where x == y: println("equal") default: break}
Swift-5-流程控制
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。