服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Swift - 我们一起聊聊Swift 条件控制和循环

我们一起聊聊Swift 条件控制和循环

2024-01-15 14:14全栈修仙之路 Swift

本文我们介绍了 Swift 中 if/else, else if, switch 和 loops 语句等相关的知识。通过与 TypeScript 语法的对比,希望能帮助您更好地理解 Swift 的相关特性。

欢迎您阅读 Mastering Swift 基础教程,本文我们将介绍 Swift 中的变量、常量和数据类型。如果你尚未安装 Xcode 和配置 Swift 开发环境,请您先阅读这篇文章。

接下来,我们启动 Xcode,然后选择 "File" > "New" > "Playground"。创建一个新的 Playground 并命名为 "ConditionalsAndLoops"。

if...else

if-else 语句是一种常见的条件控制结构,用于根据条件的真假执行不同的代码块。

Swift Code

var temperature = 28

if temperature > 30 {
    print("It's a hot day")
} else {
    print("It's not so hot")
}

// Output: It's not so hot

TypeScript Code

let temperature = 28;

if (temperature > 30) {
    console.log("It's a hot day");
} else {
    console.log("It's not so hot");
}

// Output: "It's not so hot"

if...else if...else

if...else if...else 语句允许您按顺序处理多个条件。

Swift Code

let score = 85

if score >= 90 {
    print("Excellent!")
} else if score >= 80 {
    print("Good")
} else if score >= 70 {
    print("Average")
} else {
    print("Fail")
}

// Output: Good

TypeScript Code

const score: number = 85;

if (score >= 90) {
    console.log("Excellent!");
} else if (score >= 80) {
    console.log("Good");
} else if (score >= 70) {
    console.log("Average");
} else {
    console.log("Fail");
}

// Output: Good

switch

switch 语句是一种用于处理多个可能情况的流程控制结构。在 Swift 中,switch 语句可以用于处理各种数据类型,包括整数、浮点数、字符串 等。

Swift Code

let dayOfWeek = "Wednesday"

switch dayOfWeek {
case "Monday":
    print("Start of the workweek")
case "Tuesday", "Wednesday", "Thursday":
    print("Midweek, work in progress")
case "Friday":
    print("It's Friday, almost there!")
case "Saturday", "Sunday":
    print("Weekend vibes")
default:
    print("Invalid day")
}

// Output: Midweek, work in progress

相比 JavaScript 和 TypeScript,在 Swift case 分支中,无需使用 break 跳出分支。

TypeScript Code

const dayOfWeek: string = "Wednesday";

switch (dayOfWeek) {
  case "Monday":
    console.log("Start of the workweek");
    break;
  case "Tuesday":
  case "Wednesday":
  case "Thursday":
    console.log("Midweek, work in progress");
    break;
  case "Friday":
    console.log("It's Friday, almost there!");
    break;
  case "Saturday":
  case "Sunday":
    console.log("Weekend vibes");
    break;
  default:
    console.log("Invalid day");
}

// Output: "Midweek, work in progress"

for-in

for-in 语句用于遍历集合(如数组、字典或范围)的循环结构。

Swift Code

for index in 1...5 {
    print("Index is \(index)")
}

/**
 Output:
 Index is 1
 Index is 2
 Index is 3
 Index is 4
 Index is 5
 */

在以上代码中,1...5 是一个闭区间运算符,表示一个包括从 1 到 5 的整数范围。这个范围包括 1 和 5 两个端点。

TypeScript Code

for (let index = 1; index <= 5; index++) {
    console.log(`Index is ${index}`);
}

/**
 Output:
 Index is 1
 Index is 2
 Index is 3
 Index is 4
 Index is 5
 */

在 Swift 中 for-in 循环还支持 where 子句,它可以更好地控制循环代码何时执行。

Swift Code

for index in 1...5 where index % 2 == 0 {
    print("Index is \(index)")
}

/**
 Output:
 Index is 2
 Index is 4
 */

while

while 语句是一种用于创建循环的控制流结构,只要给定条件为真,就会反复执行一段代码块。

Swift Code

var count = 1

while count <= 5 {
    print("Count is \(count)")
    count += 1
}

/**
 Output:
 Count is 1
 Count is 2
 Count is 3
 Count is 4
 Count is 5
 */

TypeScript Code

let count: number = 1;

while (count <= 5) {
    console.log(`Count is ${count}`);
    count++;
}

/**
 Output:
 Count is 1
 Count is 2
 Count is 3
 Count is 4
 Count is 5
 */

repeat-while

repeat-while 语句是一种循环结构,类似于 while 循环,不同之处在于 repeat-while 会先执行一次代码块,然后在满足条件的情况下重复执行。

Swift Code

var count = 1

repeat {
    print("Count is \(count)")
    count += 1
} while count <= 5

/**
 Output:
 Count is 1
 Count is 2
 Count is 3
 Count is 4
 Count is 5
 */

以上代码中,repeat-while 循环会先执行一次代码块,然后检查条件 count <= 5 是否仍然为真。只要条件为真,就会重复执行代码块。这确保了至少会执行一次,即使条件一开始就不满足。

在 TypeScript 中,目前并没有对应于 Swift 中 repeat-while 的语法。但可以通过 do-while 循环来实现类似的功能。

TypeScript Code

let count: number = 1;

do {
    console.log(`Count is ${count}`);
    count++;
} while (count <= 5);

/**
 Output:
 Count is 1
 Count is 2
 Count is 3
 Count is 4
 Count is 5
 */

本文我们介绍了 Swift 中 if/else, else if, switch 和 loops 语句等相关的知识。通过与 TypeScript 语法的对比,希望能帮助您更好地理解 Swift 的相关特性。


原文地址:https://mp.weixin.qq.com/s/iTLZb_DLlulBfQh1mTa95g

延伸 · 阅读

精彩推荐
  • SwiftSwift 2.1 为 UIView 添加点击事件和点击效果

    Swift 2.1 为 UIView 添加点击事件和点击效果

    本文主要介绍 Swift UIView,这里给大家提供代码示例作为参考为UIView 添加点击事件和点击效果,希望能帮助IOS开发的同学...

    Swift教程网4012021-01-22
  • SwiftSwift使用transform 实现重复平移动画效果

    Swift使用transform 实现重复平移动画效果

    这篇文章主要介绍了Swift使用transform 实现重复平移动画效果,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参...

    我为双鱼狂5022021-12-24
  • Swift浅谈Swift编程中switch与fallthrough语句的使用

    浅谈Swift编程中switch与fallthrough语句的使用

    这篇文章主要介绍了Swift编程中switch与fallthrough语句的使用,用于基本的流程控制,需要的朋友可以参考下...

    Swift教程网5612020-12-22
  • Swift详解Swift中enum枚举类型的用法

    详解Swift中enum枚举类型的用法

    Swift中通过enum关键字可以直接创建出枚举对象,而且可以使用switch和case语句来进行流程控制,十分强大和灵活,这里我们就来详解Swift中enum枚举类型的用法...

    火镜先生10162020-12-26
  • SwiftSwift实现简易计算器功能

    Swift实现简易计算器功能

    这篇文章主要为大家详细介绍了Swift实现简易计算器功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    文恒4722022-07-21
  • Swift深入理解Swift中的Substring和String

    深入理解Swift中的Substring和String

    这篇文章主要给大家深入的介绍了Swift中Substring和String的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,...

    Greg Heo5652021-04-14
  • Swiftswift计步器CMPedometer的使用方法

    swift计步器CMPedometer的使用方法

    这篇文章主要为大家详细介绍了swift计步器CMPedometer的使用方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    稻草人1122312242021-01-07
  • SwiftSwift中的高阶函数功能作用示例详解

    Swift中的高阶函数功能作用示例详解

    这篇文章主要为大家介绍了Swift中的高阶函数功能作用示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪...

    AliliWVIP6832023-03-27