如何在 FMDB + Swift 3 中处理 NULL
How to handle NULL in FMDB + Swift 3
我正在使用 fmdb,我的一些行是 NULL
在我的代码中:
let name = try database.executeQuery("select name from tb_item", values: nil)
let description = try database.executeQuery("select summary_statement from tb_item", values: nil)
let imageTry = try database.executeQuery("select main_image from tb_item", values: nil)
while name.next() && description.next() && imageTry.next() {
let title = name.string(forColumn: "name")
let description = description.string(forColumn: "summary_statement")
let imageTry = imageTry.string(forColumn: "main_image")
let list2 = modelData()
list2.title = title!
list2.description = description!
list2.image = imageTry! //fatal error: unexpectedly found nil while unwrapping an Optional value
list.append(list2)
}
如何处理 NULL 才不会出错?
创建一个图像集并将其命名为 "blank",并且不要为其指定任何图像。只需像下面这样使用它:
list2.image = imageTry ?? UIImage(named: "blank")
这意味着如果 imageTry
是 nil
那么它将为其分配空白图像。
我正在使用 fmdb,我的一些行是 NULL
在我的代码中:
let name = try database.executeQuery("select name from tb_item", values: nil)
let description = try database.executeQuery("select summary_statement from tb_item", values: nil)
let imageTry = try database.executeQuery("select main_image from tb_item", values: nil)
while name.next() && description.next() && imageTry.next() {
let title = name.string(forColumn: "name")
let description = description.string(forColumn: "summary_statement")
let imageTry = imageTry.string(forColumn: "main_image")
let list2 = modelData()
list2.title = title!
list2.description = description!
list2.image = imageTry! //fatal error: unexpectedly found nil while unwrapping an Optional value
list.append(list2)
}
如何处理 NULL 才不会出错?
创建一个图像集并将其命名为 "blank",并且不要为其指定任何图像。只需像下面这样使用它:
list2.image = imageTry ?? UIImage(named: "blank")
这意味着如果 imageTry
是 nil
那么它将为其分配空白图像。