Swift入门教程?看一篇文章就够了(五)

2021-07-08 14:27:13 浏览数 (1)

一·控制流

1.for-in

遍历数组

let names = ["swift","Object-C","golang"]

for name in names {

print("Hello (name)")

}

遍历字典

let citys = ["Guangzhou":1,"ShenZhen":2]

for a , b in citys{

print("city: (a) city:(b)")

}

遍历一段区域

for index in 1...5{}

次幂运算例子

let base = 3

let power = 10

var answer = 1

for _ in 1...power{

answer *= base

}

while循环

while (1){

//无限循环

}

repeat {

//循环前先执行一次代码

}while (condition)

神似do-while

if判断

if (条件){}

Switch 判断

switch ()

case:

case:

default:

guard提前退出

guard bool else{ print() }

不同于if语句,guard永远有一个else语句,如果条件不为真那么执行else

0 人点赞