奇思妙想 · 2024年 5月 2日 0

如何理解Swift “case let xxx:”?

7 次浏览

一样,我们可以找到Swift switch语法定义:Switch Statements | swift.org

case-label → attributes? case case-item-list :
case-item-list → pattern where-clause? | pattern where-clause? , case-item-list

其实,let xxx是一种idenfifier pattern (Identifier Patterns | swift.org), 如下:

pattern → wildcard-pattern type-annotation?
pattern → identifier-pattern type-annotation?
pattern → value-binding-pattern
pattern → tuple-pattern type-annotation?
pattern → enum-case-pattern
pattern → optional-pattern
pattern → type-casting-pattern
pattern → expression-pattern