swift控制流程迅速(代码片段)

author author     2023-01-20     467

关键词:

//: ## Control Flow
//:
//: Use `if` and `switch` to make conditionals, and use `for`-`in`, `for`, `while`, and `repeat`-`while` to make loops. Parentheses around the condition or loop variable are optional. Braces around the body are required.
//:
let individualScores = [75, 43, 103, 87, 12]
var teamScore = 0
for score in individualScores 
    if score > 50 
        teamScore += 3
     else 
        teamScore += 1
    

print(teamScore)

//: In an `if` statement, the conditional must be a Boolean expression—this means that code such as `if score  ... ` is an error, not an implicit comparison to zero.
//:
//: You can use `if` and `let` together to work with values that might be missing. These values are represented as optionals. An optional value either contains a value or contains `nil` to indicate that a value is missing. Write a question mark (`?`) after the type of a value to mark the value as optional.
//:
var optionalString: String? = "Hello"
print(optionalString == nil)

var optionalName: String? = "John Appleseed"
var greeting = "Hello!"
if let name = optionalName 
    greeting = "Hello, \(name)"


//: > **Experiment**:
//: > Change `optionalName` to `nil`. What greeting do you get? Add an `else` clause that sets a different greeting if `optionalName` is `nil`.
//:
//: If the optional value is `nil`, the conditional is `false` and the code in braces is skipped. Otherwise, the optional value is unwrapped and assigned to the constant after `let`, which makes the unwrapped value available inside the block of code.
//:
//: Another way to handle optional values is to provide a default value using the `??` operator. If the optional value is missing, the default value is used instead.
//:
let nickName: String? = nil
let fullName: String = "John Appleseed"
let informalGreeting = "Hi \(nickName ?? fullName)"

//: Switches support any kind of data and a wide variety of comparison operations—they aren’t limited to integers and tests for equality.
//:
let vegetable = "red pepper"
switch vegetable 
    case "celery":
        print("Add some raisins and make ants on a log.")
    case "cucumber", "watercress":
        print("That would make a good tea sandwich.")
    case let x where x.hasSuffix("pepper"):
        print("Is it a spicy \(x)?")
    default:
        print("Everything tastes good in soup.")


//: > **Experiment**:
//: > Try removing the default case. What error do you get?
//:
//: Notice how `let` can be used in a pattern to assign the value that matched that part of a pattern to a constant.
//:
//: After executing the code inside the switch case that matched, the program exits from the switch statement. Execution doesn’t continue to the next case, so there is no need to explicitly break out of the switch at the end of each case’s code.
//:
//: You use `for`-`in` to iterate over items in a dictionary by providing a pair of names to use for each key-value pair. Dictionaries are an unordered collection, so their keys and values are iterated over in an arbitrary order.
//:
let interestingNumbers = [
    "Prime": [2, 3, 5, 7, 11, 13],
    "Fibonacci": [1, 1, 2, 3, 5, 8],
    "Square": [1, 4, 9, 16, 25],
]
var largest = 0
for (kind, numbers) in interestingNumbers 
    for number in numbers 
        if number > largest 
            largest = number
        
    

print(largest)

//: > **Experiment**:
//: > Add another variable to keep track of which kind of number was the largest, as well as what that largest number was.
//:
//: Use `while` to repeat a block of code until a condition changes. The condition of a loop can be at the end instead, ensuring that the loop is run at least once.
//:
var n = 2
while n < 100 
    n = n * 2

print(n)

var m = 2
repeat 
    m = m * 2
 while m < 100
print(m)

//: You can keep an index in a loop—either by using `..<` to make a range of indexes or by writing an explicit initialization, condition, and increment. These two loops do the same thing:
//:
var firstForLoop = 0
for i in 0..<4 
    firstForLoop += i

print(firstForLoop)

var secondForLoop = 0
for var i = 0; i < 4; ++i 
    secondForLoop += i

print(secondForLoop)

swift迅速的时机(代码片段)

查看详情

swift你好世界迅速(代码片段)

查看详情

swift协议和扩展迅速(代码片段)

查看详情

swift代表团迅速1(代码片段)

查看详情

swift细胞自动机动作迅速(代码片段)

查看详情

swift学习——格式控制符和元组流程控制(代码片段)

Swift中的格式控制符和元祖(1)首先介绍一下元祖,元祖是关系型数据库中?比如学生表中的姓名,年龄,电话等定义例如以下varstudentinfo=("jhon",29,"123456")println(studentinfo.0)訪问的时候是通过下标来訪问的。//元组本身... 查看详情

swift语句参考!(代码片段)

在Swift中,有两种类型的语句:简单语句和控制流语句。简单语句是最常见的,用于构造表达式和声明。控制流语句则用于控制程序执行的流程,Swift中有三种类型的控制流语句:循环语句、分支语句和控制传... 查看详情

swift语句参考!(代码片段)

在Swift中,有两种类型的语句:简单语句和控制流语句。简单语句是最常见的,用于构造表达式和声明。控制流语句则用于控制程序执行的流程,Swift中有三种类型的控制流语句:循环语句、分支语句和控制传... 查看详情

swift-02代码流程的控制

//// main.swift// 02-语句//// Createdbywanghyon15/8/9.// Copyright(c)2015年wanghy.Allrightsreserved.//importFoundation//1.//使用分支循环控制代码的流程//分支:if  if-else  switch// 查看详情

swift控制流(代码片段)

查看详情

swift警报控制器(代码片段)

查看详情

swift自定义分段控制(代码片段)

查看详情

swift应用程序的swift控制器(代码片段)

查看详情

swift运算符循环流程控制for-in,while,if-else,switch-case,guard-else(代码片段)

Swift运算符、循环、流程控制for-in,while,if-else,switch-case,guard-else1.运算符:三目,空合并,区间运算符//元组比较大小。需要元素个数一致,对应的位置的元素类型相同,每一个元素都必须支持比较运算操作。//... 查看详情

swift表格导航控制器(代码片段)

查看详情

swift来自xib的控制器(代码片段)

查看详情

swift后父控制器时调用函数(代码片段)

查看详情

swift以编程方式设置初始控制器(代码片段)

查看详情