zoukankan      html  css  js  c++  java
  • swift 有道 翻译文档(2 条件语句 循环语句)

    控制流
    使用if和switch来创建条件语句,使用for-in、while和repeat-while来创建循环。条件或循环变量的括号是可选的。身体周围需要支撑。

    let individualScores = [75, 43, 103, 87, 12]
    var teamScore = 0
    for score in individualScores {
    if score > 50 {
    teamScore += 3
    } else {
    teamScore += 1
    }
    }
    print(teamScore)

    在if语句中,条件必须是布尔表达式——这意味着if score{…}是一个错误,而不是与零的隐式比较。
    您可以使用if和let一起处理可能丢失的值。这些值表示为选项。可选值包括"一个值或包含nil以表示缺少一个值。在值的类型后面写一个问号(?),将值标记为可选的。

    var optionalString: String? = "Hello"
    print(optionalString == nil)

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

    实验
    改变optionalName为零。你会得到什么问候?如果optionalName是nil,那么添加一个else子句来设置不同的问候语。

    如果可选值为nil,条件为false,大括号中的代码将被跳过。否则,可选值将被解包并分配给let后的常量,这使得解包值在代码块中可用

    处理可选值的另一种方法是使用??操作符。如果缺少可选值,则使用默认值。

    let nickName: String? = nil
    let fullName: String = "John Appleseed"
    let informalGreeting = "Hi (nickName ?? fullName)

    交换机支持任何类型的数据和各种各样的比较操作——它们不限于整数和相等的测试

    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.")
    }

    摘录来自: Apple Inc. “The Swift Programming Language (Swift 4)。” iBooks.

    完整翻译链接 http://www.swift51.com/swift4.0/chapter1/02_a_swift_tour.html

    如有侵权请联系本人,谢谢

  • 相关阅读:
    SSWA jsp 函数 的页面
    fun_function.execute
    pa_transaction_interface_all
    EBS mo_glob_org_access_tmp表的分析
    R12将银行和分行都使用TCA管理
    ap_invoice_distributions_all到xla_ae_lines
    python使用uuid和guid
    js http请求
    DES加密
    wxPython操作图形用户界面
  • 原文地址:https://www.cnblogs.com/ccw-congcong/p/9474690.html
Copyright © 2011-2022 走看看