"case" 是什么意思Swift 中没有 switch 语句是什么意思? [英] What does "case" mean without switch statement in Swift?

查看:41
本文介绍了"case" 是什么意思Swift 中没有 switch 语句是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了这段代码,它是 Swift 算法俱乐部.在整个实现过程中,作者使用 case let 紧跟一个 while 语句,然后再解开一个可选项.我从未见过在 switch 语句上下文之外使用的 case 关键字,我想知道它到底是做什么的?它是否以某种方式投射 let next?= node.next 部分为 true 或 false,可能取决于 next? 是否变为 nil?

I ran into this code which is part of a Swift implementation of a linked list in the Swift Algorithm Club. Throughout the implementation the author uses case let following a while statement right before unwrapping an optional. I've never seen the case keyword used outside of the context of a switch statement, and I'm wondering what exactly it does? Does it somehow cast the let next? = node.next part to true or false, maybe depending on whether next? becomes nil or not?

public var last: Node? {
    if var node = head {
        while case let next? = node.next {
            node = next
        }
        return node
    } else {
        return nil
    }
}

推荐答案

这是可选模式.它测试并解包 Optional,仅当 Optional 非零时才执行条件.

This is the Optional Pattern. It tests and unwraps an Optional, executing the conditional only if the Optional is non-nil.

需要关键字case,因为它遵循原始的switch...case 语法.case 测试模式,如果匹配,则执行以下语句.在您的示例中, let next? 是模式.如果该值已解包并分配,则 case 匹配并执行您的代码.

The keyword case is needed because it follows from the original switch...case syntax. The case tests a pattern and if it matches then the following statement is executed. In your example the let next? is the pattern. If the value is unwrapped and assigned then the case matches and your code is executed.

来自文档:

可选模式

可选模式匹配包含在 Some(Wrapped) 情况下的值一个 Optional 或 ImplicitlyUnwrappedOptional枚举.可选模式由标识符模式组成紧随其后的是一个问号并出现在相同的地方作为枚举案例模式.

An optional pattern matches values wrapped in a Some(Wrapped) case of an Optional or ImplicitlyUnwrappedOptional enumeration. Optional patterns consist of an identifier pattern followed immediately by a question mark and appear in the same places as enumeration case patterns.

因为可选模式是 Optional 和ImplicitlyUnwrappedOptional 枚举 case 模式,如下是等价的:

Because optional patterns are syntactic sugar for Optional and ImplicitlyUnwrappedOptional enumeration case patterns, the following are equivalent:

let someOptional: Int? = 42
// Match using an enumeration case pattern
if case .Some(let x) = someOptional {
    print(x)
}

// Match using an optional pattern
if case let x? = someOptional {
    print(x)
}

可选模式提供了一种方便的方式来迭代for-in 语句中的可选值数组,执行循环仅适用于非 nil 元素.

The optional pattern provides a convenient way to iterate over an array of optional values in a for-in statement, executing the body of the loop only for non-nil elements.

let arrayOfOptionalInts: [Int?] = [nil, 2, 3, nil, 5]
// Match only non-nil values
for case let number? in arrayOfOptionalInts {
    print("Found a \(number)")
}
// Found a 2
// Found a 3
// Found a 5

这篇关于"case" 是什么意思Swift 中没有 switch 语句是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆