Ios Swift3,展开可选<;装订>;(在SQLite.swift中)
在最优秀的SQLite.swift中,我Ios Swift3,展开可选<;装订>;(在SQLite.swift中),ios,swift3,optional,optional-binding,Ios,Swift3,Optional,Optional Binding,在最优秀的SQLite.swift中,我 let stmt = try local.db!.prepare(..) for row in stmt { for tricky in row { 每个“技巧”都是一个可选的 我所知道的解开每一个棘手问题的唯一方法就是这样 变量可打印:字符串=” 在这个例子中,我非常确定它只能是一个Int64或字符串(或指向字符串的东西);我想可能需要使用默认情况来涵盖其中的任何其他可能性 有更快捷的方法吗 是否有方法获取可选的类型 (顺便说一句,特别是
let stmt = try local.db!.prepare(..)
for row in stmt {
for tricky in row {
每个“技巧”都是一个可选的
我所知道的解开每一个棘手问题的唯一方法就是这样
变量可打印:字符串=”
在这个例子中,我非常确定它只能是一个Int64或字符串(或指向字符串的东西);我想可能需要使用默认情况来涵盖其中的任何其他可能性
有更快捷的方法吗
是否有方法获取可选的类型
(顺便说一句,特别是关于SQLite.swift;从doco中可能有一种我不知道的方法“获取列n的类型”。这很酷,但是,通常上一段中的问题仍然存在。)可以基于类使用switch语句。此类switch语句的示例代码如下所示:
let array: [Any?] = ["One", 1, nil, "Two", 2]
for item in array {
switch item {
case let anInt as Int:
print("Element is int \(anInt)")
case let aString as String:
print("Element is String \"\(aString)\"")
case nil:
print("Element is nil")
default:
break
}
}
如果需要,您还可以将where子句添加到一个或多个案例中:
let array: [Any?] = ["One", 1, nil, "Two", 2, "One Hundred", 100]
for item in array {
switch item {
case let anInt as Int
where anInt < 100:
print("Element is int < 100 == \(anInt)")
case let anInt as Int where anInt >= 100:
print("Element is int >= 100 == \(anInt)")
case let aString as String:
print("Element is String \"\(aString)\"")
case nil:
print("Element is nil")
default:
break
}
}
let数组:[任何?]=[“一”,1,零,“两”,2,“一百”,100]
用于数组中的项{
开关项{
case-let-anInt为Int
其中anInt<100:
打印(“元素为int<100=\(anInt)”)
将anInt设为Int,其中anInt>=100:
打印(“元素为int>=100==\(anInt)”)
以字符串形式显示的大小写:
打印(“元素为字符串\”(aString)\”)
无案例:
打印(“元素为零”)
违约:
打破
}
}
您可以使用switch语句,例如右图:您知道,邓肯解释的“nil case”可能非常有用
let array: [Any?] = ["One", 1, nil, "Two", 2, "One Hundred", 100]
for item in array {
switch item {
case let anInt as Int
where anInt < 100:
print("Element is int < 100 == \(anInt)")
case let anInt as Int where anInt >= 100:
print("Element is int >= 100 == \(anInt)")
case let aString as String:
print("Element is String \"\(aString)\"")
case nil:
print("Element is nil")
default:
break
}
}