Swift for-in循环语句介绍和用法示例

Swift For-in循环用于运行一组任务一定次数。此循环会重复执行多次。
例如
如果要编写一个打印” Hello srcmini” 的语法, 则简单语法为:

print("Hello srcmini")

如果你必须写相同的打印语句5次, 那会是什么?简单的解决方案是:
print("Hello srcmini") print("Hello srcmini") print("Hello srcmini") print("Hello srcmini") print("Hello srcmini")

但是, 有一种解决方案。你可以使用for-in循环来打印相同的语句并使代码更短。
for i in 1...5 {//It will print Hello srcmini for 5 times on the screenprint("Hello srcmini")}

范例1:
let names = ["Ajeet", "Aryan", "Ayan", "Alex"]for name in names {print("Good Morning \(name)")}

输出
Good Morning AjeetGood Morning AryanGood Morning AyanGood Morning Alex

【Swift for-in循环语句介绍和用法示例】一个Swift程序, 用于打印特定编号的表(此处为5)。

for index in 1...10 {print("\(index) times 5 is \(index * 5)")}

输出
1 times 5 is 52 times 5 is 103 times 5 is 154 times 5 is 205 times 5 is 256 times 5 is 307 times 5 is 358 times 5 is 409 times 5 is 4510 times 5 is 50

    推荐阅读