Swift语法基础:2 - Swift的控制流简介

Swift语法基础:2 - Swift的控制流简介,第1张

概述前面我们已经大致了解了Swift的一些代码结构, 声明的时候怎么用, 字符串的串联是怎么回事, 现在我们来看看那Swift的控制流, 所谓的控制流就是for-in, for, switch, if, while, do-while, 下面让我们来看例子: 1.for-in的使用 let individualScores = [1, 2, 3, 4, 5, 6]for i in individu

前面我们已经大致了解了Swift的一些代码结构,声明的时候怎么用,字符串的串联是怎么回事,现在我们来看看那Swift的控制流,所谓的控制流就是for-in,for,switch,if,while,do-while,下面让我们来看例子:

1.for-in的使用
let indivIDualscores = [1,2,3,4,5,6]for i in indivIDualscores {    println(i)}// 打印出来的结果:1,2,3,4,5,6

我们也可以在for-in里添加一个判断语句:

var num = 0for i in indivIDualscores {    if i < 3{        num++        println("num is \(num)")    } else {        num--        println("num is \(num)")    }}// 打印出来的结果: num is 1,num is 2,num is 1,num is 0,num is -1,num is -2

并且我们还可以使用for-in来遍历字典,这个遍历会非常的爽:

let interestingNumbers = [        "Prime": [2,7,11,13],"Fibonacci": [1,1,8],"Square": [1,9,16,25],]var largest = 0for (kind,numbers) in interestingNumbers {    println(kind)    println(numbers)}// 打印出来的结果: Prime// [2,7,11,13]// Fibonacci// [1,1,8]// Square// [1,9,16,25]

我们也可以只遍历整个字典最大的值:

for (kind,numbers) in interestingNumbers {    for @H_502_138@number in numbers {        if @H_502_138@number > largest {            largest = @H_502_138@number        }    }} // 打印出来的结果是:25
2.1if,let还有” ? “嵌套使用
var optionalString: String? = "Hello"    optionalString == nilvar optionalname: String? = "John Appleseed"var greeting = "Hello!"if let name = optionalname {    greeting = "Hello,\(name)"}println("optionalString is \(optionalString),optionalname is \(optionalname),greeting is \(greeting)")// 打印出来的结果:optionalString is Optional("Hello"),optionalname is Optional("John Appleseed"),greeting is Hello,John Appleseed

当我们改改例子的东西,出来的结果就不一样了:

var optionalString: String? = "Hello"    optionalString == nilvar optionalname: String? = "John Appleseed"    optionalname == nilvar greeting = "Hello!"if let name = optionalname {    greeting = "Hello,\(name)"}if let name1 = optionalname {    greeting = "xiaoming"}println("optionalString is \(optionalString),greeting is \(greeting)")// 打印出来的结果:optionalString is Optional("Hello"),greeting is xiaoming
3.switch的用法
let vegetable = "red pepper"switch vegetable {    case "celery ":        let vegetableComment = "Add some raisins and make ants on a log."        println(vegetableComment)    case "cucumber","watercress":        let vegetableComment = "That would make a good tea sanDWich."        println(vegetableComment)    case let x where x.hasSuffix("pepper"):        let vegetableComment = "Is it a spicy \(x)?"        println(vegetableComment)    default:        let vegetableComment = "Everything tastes good in soup."        println(vegetableComment)}// 打印出来的结果: Is it a spicy red pepper?

如果我们把default给删除掉就会出现一个错误:

// Switch must be exhaustive,consIDer adding a default clause提示说开关一定要有一个default,否则该开关就不会成立

PS: 在Swift中,Switch执行到匹配的那一行就会自动退出,并不会继续往下执行,所以这里就不存在break.

4.while以及do-while使用
var n = 2while n<100 {    n=n*2}var m = 2do {    m=m*2} while m < 100println("n is \(n),m is \(m)")// 打印出来的结果: n is 128,m is 128
5.补充一点知识
var firstForLoop = 0for i in 0...3{        firstForLoop += i    }var secondForLoop = 0for var i=0; i<3; ++i {    secondForLoop += 1}println("\(firstForLoop),\(secondForLoop)")// 打印出来的结果: 6,3

看到第一个例子的时候,其实它是和第二个例子是相等的,这是一个区间,0 ~ 3这之间,所以在Swift开发时候,无论是传统的写法,还是新的写法都是可以使用的.

好了这次我们就讲到这里,下次我们继续

总结

以上是内存溢出为你收集整理的Swift语法基础:2 - Swift的控制流简介全部内容,希望文章能够帮你解决Swift语法基础:2 - Swift的控制流简介所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1088838.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存