我是 swift 的新手,刚刚学习了 sqlite 教程,它运行得很好,但在升级到 swift 2.1 后,它在我的结果中附加了“可选”,我该如何摆脱它?
查找数据功能
@IBAction func findContact(sender: UIButton)
{
if database.open() {
let querySQL = "SELECT address, phone FROM CONTACTS WHERE name = '\(name.text)'"
let results:FMResultSet? = database.executeQuery(querySQL,withArgumentsInArray: nil)
if results!.next() == true {
if let address1=results!.stringForColumn("address")
{
address.text = address1
}
phone.text = results!.stringForColumn("phone")
status.text = "Record Found"
} else {
status.text = "Record not found"
address.text = ""
phone.text = ""
}
database.close()
} else {
print("Error: \(database.lastErrorMessage())", terminator: "")
}
}
保存数据功能
@IBAction func saveData(sender: UIButton)
{
let database = FMDatabase(path: databasePath as String)
if database.open() {
let insertSQL = "INSERT INTO CONTACTS (name, address, phone) VALUES ('\(name.text)', '\(address.text)', '\(phone.text)')"
let result = database.executeUpdate(insertSQL,
withArgumentsInArray: nil)
if !result {
status.text = "Failed to add contact"
print("Error: \(database.lastErrorMessage())", terminator: "")
} else {
status.text = "Contact Added"
name.text = ""
address.text = ""
phone.text = ""
}
} else {
print("Error: \(database.lastErrorMessage())", terminator: "")
}
}
最佳答案
问题不在于您选择数据时。问题在于您插入数据时。在插入时,您只需在表格中插入可选值,如地址字段和电话号码,如屏幕截图所示...所以,在插入时,您需要解包该可选值以避免 optional()
关键字。
关于ios - 升级到 swift 2.1 后,Swift 返回可选的 ("value"),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33450050/