需要澄清 Swift 中的类型转换运算符
Need clarification of Type Casting operator in Swift
为什么在此 switch 语句中使用类型转换运算符 (as) 而不是其条件形式 (as?)?
我认为类型运算符只能是 (as?) 或 (as!)...? Apple Swift 文档对此没有提供充分的解释。
这是 Swift 文档中的示例:
var things = [Any]()
things.append(0)
things.append(0.0)
things.append(42)
things.append(3.14159)
things.append("hello")
things.append((3.0, 5.0))
things.append(Movie(name: "Ghostbusters", director: "Ivan Reitman"))
things.append({ (name: String) -> String in "Hello, \(name)" })
for thing in things {
switch thing {
case 0 as Int:
println("zero as an Int")
case 0 as Double:
println("zero as a Double")
case let someInt as Int:
println("an integer value of \(someInt)")
case let someDouble as Double where someDouble > 0:
println("a positive double value of \(someDouble)")
case is Double:
println("some other double value that I don't want to print")
case let someString as String:
println("a string value of \"\(someString)\"")
case let (x, y) as (Double, Double):
println("an (x, y) point at \(x), \(y)")
case let movie as Movie:
println("a movie called '\(movie.name)', dir. \(movie.director)")
case let stringConverter as String -> String:
println(stringConverter("Michael"))
default:
println("something else")
}
}
如果您阅读底部的 note,您可能会自己找到答案:
The cases of a switch statement use the forced version of the type cast operator (as, not as?) to check and cast to a specific type. This check is always safe within the context of a switch case statement.
(强调我的)
Here 是一个 Apple 博客 post,它详细说明了 as?
、as
和 as!
之间的区别。
case 0 as Int:
或case let someInt as Int:
中的as
是其一部分
一个 类型的铸造模式 。在Swift Language Reference switch 语句的case 标签中
定义为
case-label → case case-item-list:
case-item-list →
pattern guard-clauseopt | pattern guard-clauseopt , case-item-list
模式可以在哪里(在其他情况下)
pattern → value-binding-pattern
pattern → type-casting-pattern
pattern → expression-pattern
类型转换模式是
type-casting-pattern → is-pattern | as-pattern
is-pattern → is type
as-pattern → pattern as type
例如你有
case let someInt as Int:
╰──────────────────────╯ case-label
╰────────────────╯ case-item-list -> type-casting pattern
╰─╯ type
╰╯ `as` keyword
╰─────────╯ value-binding pattern
Swift-博客:
It may be easiest to remember the pattern for these operators in Swift as: ! implies “this might trap,” while ? indicates “this might be nil.”
还有第三种保证转换的可能性,当向上转换完成时。例如 2 as Any
在与 as!
或 as?
一起使用时会收到警告
在 switch 构造的情况下,case let value as Type:
永远不会失败,也不可能,与表达式 value as? Type
相比,该值将是可选类型
为什么在此 switch 语句中使用类型转换运算符 (as) 而不是其条件形式 (as?)?
我认为类型运算符只能是 (as?) 或 (as!)...? Apple Swift 文档对此没有提供充分的解释。
这是 Swift 文档中的示例:
var things = [Any]()
things.append(0)
things.append(0.0)
things.append(42)
things.append(3.14159)
things.append("hello")
things.append((3.0, 5.0))
things.append(Movie(name: "Ghostbusters", director: "Ivan Reitman"))
things.append({ (name: String) -> String in "Hello, \(name)" })
for thing in things {
switch thing {
case 0 as Int:
println("zero as an Int")
case 0 as Double:
println("zero as a Double")
case let someInt as Int:
println("an integer value of \(someInt)")
case let someDouble as Double where someDouble > 0:
println("a positive double value of \(someDouble)")
case is Double:
println("some other double value that I don't want to print")
case let someString as String:
println("a string value of \"\(someString)\"")
case let (x, y) as (Double, Double):
println("an (x, y) point at \(x), \(y)")
case let movie as Movie:
println("a movie called '\(movie.name)', dir. \(movie.director)")
case let stringConverter as String -> String:
println(stringConverter("Michael"))
default:
println("something else")
}
}
如果您阅读底部的 note,您可能会自己找到答案:
The cases of a switch statement use the forced version of the type cast operator (as, not as?) to check and cast to a specific type. This check is always safe within the context of a switch case statement.
(强调我的)
Here 是一个 Apple 博客 post,它详细说明了 as?
、as
和 as!
之间的区别。
case 0 as Int:
或case let someInt as Int:
中的as
是其一部分
一个 类型的铸造模式 。在Swift Language Reference switch 语句的case 标签中
定义为
case-label → case case-item-list:
case-item-list → pattern guard-clauseopt | pattern guard-clauseopt , case-item-list
模式可以在哪里(在其他情况下)
pattern → value-binding-pattern
pattern → type-casting-pattern
pattern → expression-pattern
类型转换模式是
type-casting-pattern → is-pattern | as-pattern
is-pattern → is type
as-pattern → pattern as type
例如你有
case let someInt as Int:
╰──────────────────────╯ case-label
╰────────────────╯ case-item-list -> type-casting pattern
╰─╯ type
╰╯ `as` keyword
╰─────────╯ value-binding pattern
Swift-博客:
It may be easiest to remember the pattern for these operators in Swift as: ! implies “this might trap,” while ? indicates “this might be nil.”
还有第三种保证转换的可能性,当向上转换完成时。例如 2 as Any
在与 as!
或 as?
在 switch 构造的情况下,case let value as Type:
永远不会失败,也不可能,与表达式 value as? Type