【发布时间】:2016-12-19 21:10:05
【问题描述】:
在最优秀的SQLite.swift中,我有
let stmt = try local.db!.prepare(..)
for row in stmt {
for tricky in row {
每个“棘手”都是Optional<Binding>
我知道解开每个棘手的唯一方法是这样
var printable:String = "
if let trickyNotNil = tricky {
if let anInt:Int64 = trickyNotNil as? Int64 {
print("it's an Int")
.. use anInt
}
if let aString:String = trickyNotNil as? String {
print("it's a String")
.. use aString}
}
else {
tricky is nil, do something else
}
在示例中,我很确定它只能是 Int64 或 String(或转至 String 的东西);我想人们可能不得不用默认情况来涵盖其中的任何其他可能性。
有没有更快捷的方法?
有没有办法获取Optional<Binding> 的类型?
(顺便说一句,特别是关于 SQLite.swift;我可能从 doco 中不知道有一种方法可以“获取 n 列的类型”。这很酷,但是,上一段中的问题仍然存在,一般。)
【问题讨论】:
-
你可以使用 switch 语句,例如Pattern match and conditionally bind in a single Switch statement
-
正确:你知道,DuncanC 解释的“nil case”非常有用
标签: ios swift3 optional optional-binding