swift5(5)流程控制
2020-02-26 本文已影响0人
C_G__
感谢作者
https://www.jianshu.com/p/5fa0e1c00225
//
// For-In循环
let names = ["Anna", "Alex", "Brian", "Jack"];
for name in names {
print("Hello, \(name)!");
}
let airports51 = ["job": "将军", "age": "16", "name": "zhangfei"];
for (key,value) in airports51 {
print("\(key)");
print("\(value)");
}
for index in 1...5 {
print("\(index) times 5 is \(index * 5)");
}
// 符合Strideable协议的任何类型的值都可以使用此函数
// 例如整数或浮点类型。
// 不包含结束值
for item in stride(from: 0, to: 60, by: 5) {
print(item, separator: "", terminator: " ");
//!< 0 5 10 15 20 25 30 35 40 45 50 55
}
for item in stride(from: 0, to: Double.pi * 2, by: .pi/2) {
print(item, separator: "", terminator: " ")//!<0.0 1.5707963267948966 3.141592653589793 4.71238898038469
}
// 区别于by,可能会包含结束值
for item in stride(from: 0, through: 60, by: 5) {
print(item, separator: "", terminator: " ");
//!< 0 5 10 15 20 25 30 35 40 45 50 55 60
}
for item in stride(from: 0, through: Double.pi * 2, by: .pi/2) {
print(item, separator: "", terminator: " ");
//!<0.0 1.5707963267948966 3.141592653589793 4.71238898038469 6.283185307179586
}
// While循环
var adc = 10;
var apc = 0;
while apc < adc {
apc += 1;
//!< 必须要有终止的条件 若 apc < adc 恒成立则程序陷入死循环
print("条件成立"); //10
}
adc = 10;
apc = 0;
repeat {
apc += 1;
print("条件成立"); //!< 10次
} while apc < adc
// if
adc = 20;
if adc <= 32 {
print("adc <= 32");
} else if adc >= 86 {
print("adc >= 86");
} else {
print("32 <adc< 86");
}
// switch
let someCharacter: Character = "z";
switch someCharacter {
case "a":
print("The first letter of the alphabet");
case "z":
print("The last letter of the alphabet");
default:
print("Some other character");
}
let anotherCharacter: Character = "a";
switch anotherCharacter {
case "a", "A":
print("The letter A");
default:
print("Not the letter A");
}
// Range匹配
adc = 20;
apc = 0;
var result = "";
switch adc {
case 0...10:
result = "0...10";
case 11..<20:
result = "11..<20";
case 20...30:
result = "20...30";
default:
result = "beyond of range";
}
print(result);//!< 20...30
// 元组
let tuples : (Int,String,Int) = (404,"not found",-1);
switch tuples {
case (300,"精准匹配1",0):
print("精准匹配1");
case (0...200,"范围匹配1",-2...10):
print("范围匹配1");
case (_,_,-2...2):
print("通配符匹配元组前两个,范围匹配最后一项");
default:
print("没有匹配到");
}
// 值绑定
let anotherPoint = (2, 0);
switch anotherPoint {
case (let xx, 0):
print("横坐标\(xx)");
case (0, let yy):
print("纵坐标 \(yy)");
case let (xx, yy):
print("任何点 (\(xx), \(yy))");
}
// Where
let yetAnotherPoint = (1, -1);
switch yetAnotherPoint {
case let (x, y) where x==y:
print("匹配到x与y相同的情况");
case let(x,y) where x == -y:
print("匹配到x与y为相反数的情况");
//!<匹配到x与y为相反数的情况
default:
print("没有匹配的结果");
}
// 复合使用
let someCharater = "e";
switch someCharater {
case "a","o","e","f":
print("事例1,匹配成功");//!< log
case "b","v","r","h":
print("事例2,匹配成功");
default:
print("未匹配");
}
//复合事例中的值绑定,绑定的值类型必须匹配。case (let x, let y), (0, let x)这种是不被允许的 因为方法体中使用x,y时,若匹配的是 (0, let x)则y 无效。因为相同的值绑定应该存在于所有`case`之后的模式中。
let stillAnotherPoint = (9, 0);
switch stillAnotherPoint {
case (let x, 0), (0, let x):
//!< 'x' must be bound in every pattern:'x'必须绑定在每个模式中
print("匹配成功");
default:
print("未匹配");
}
// 控制转移语句
// continue
let puzzleInput = "great minds think alike";
var puzzleOutput = "";
let charactersToRemove: [Character] = ["a", "e", "i", "o", "u", " "];
for character in puzzleInput {
if charactersToRemove.contains(character) {
continue;
}
puzzleOutput.append(character);
}
print(puzzleOutput);
//!< break下跳出循环,立即开始下一次迭代,输出为:grtmndsthnklk
// break
for character in puzzleInput {
if charactersToRemove.contains(character) {
break;
}
puzzleOutput.append(character);
}
print(puzzleOutput);
//!< break下跳出循环不在开始,输出为gr
// 贯穿(Fallthrough)
let describe = 5;
var description = "";
switch describe {
case 2, 3, 5, 7, 11, 13, 17, 19:
description += "I am";
fallthrough;
case 18:
description += " bob";
//!< case 去掉fallthrough 则输出I am bob
fallthrough;
default:
description += " and you?";
}
print(description);
//!< I am bob and you?
// 标签语句
result = "";
adc = 30;
forLabel : for item in 9...adc {
switchLabel :switch item {
case 0...10:
result += " 0...10";
break switchLabel; //!<swift中break是默认的
case 11..<20: //!< 若是11..<20区间则跳出for循环,开始下次迭代
continue forLabel;
case 20://!< 若是20 则立即终止for循环
result += " 终止for循环";
break forLabel;
default:
result += "beyond of range";
}
}
print(result);
//!< 0...10 0...10 终止for循环
// 提前退出(throw,return)
// 与if语句一样,guard语句根据表达式的布尔值执行语句。
// 使用guard语句要求条件必须为true才能执行guard语句之后的代码。
// 与if语句不同,guard语句总是有一个else子句
// 如果条件为false,则执行else子句中的代码。
// guard的else子句不能向下贯穿,
// 必须使用转移控制的语句return或throw才能退出作用域。
// adc = 30;
// //! guard方法体不能向下贯穿,
// // 需要使用`return`或`throw`退出作用域
// guard adc > 30 else {
// print("条件不成立");
// return; //!< 提前结束了
// }
// //throw退出作用域
// guard adc > 30 else {
// print("条件不成立");
// throw HttpError.authError;
// }
// 检查API可用性
if #available(iOS 10, macOS 10.12, *) {
// Use iOS 10 APIs on iOS, and use macOS 10.12 APIs on macOS
} else {
// Fall back to earlier iOS and macOS APIs
}