我正在关注iOS应用开发期间使用数据库的文本教程(http://www.techotopia.com/index.php/An_Example_SQLite_based_iOS_8_Application_using_Swift_and_FMDB)
在下面的代码中:
databasePath = docsDir.stringByAppendingPathComponent("contacts.db")
出现错误:
'stringByAppendingPathComponent'不可用:改为在NSURL上使用URLByAppendingPathComponent。
当我尝试这个建议时:
databasePath = NSURL(fileURLWithPath:NSTemporaryDirectory()).URLByAppendingPathComponent("contacts.db")
我意识到通过进行修改,我们现在正在使用URL而不是字符串,但我不知道对代码做了哪些其他更改
完整代码:
import UIKit
类ViewController:UIViewController {
@IBOutlet weak var name: UITextField!
@IBOutlet weak var address: UITextField!
@IBOutlet weak var phone: UITextField!
@IBOutlet weak var status: UILabel!
var databasePath = NSString()
override func viewDidLoad() {
super.viewDidLoad()
let filemgr = NSFileManager.defaultManager()
let dirPaths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory,.UserDomainMask, true)
let docsDir = dirPaths[0] as! String
databasePath = docsDir.stringByAppendingPathComponent(
"contacts.db")
if !filemgr.fileExistsAtPath(databasePath as String) {
let contactDB = FMDatabase(path: databasePath as String)
if contactDB == nil {
print("Error: \(contactDB.lastErrorMessage())")
}
if contactDB.open() {
let sql_stmt = "CREATE TABLE IF NOT EXISTS CONTACTS (ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, ADDRESS TEXT, PHONE TEXT)"
if !contactDB.executeStatements(sql_stmt) {
print("Error: \(contactDB.lastErrorMessage())")
}
contactDB.close()
} else {
print("Error: \(contactDB.lastErrorMessage())")
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func saveData(sender: AnyObject) {
let contactDB = FMDatabase(path: databasePath as String)
if contactDB.open() {
let insertSQL = "INSERT INTO CONTACTS (name, address, phone) VALUES ('\(name.text)', '\(address.text)', '\(phone.text)')"
let result = contactDB.executeUpdate(insertSQL,
withArgumentsInArray: nil)
if !result {
status.text = "Failed to add contact"
print("Error: \(contactDB.lastErrorMessage())")
} else {
status.text = "Contact Added"
name.text = ""
address.text = ""
phone.text = ""
}
} else {
print("Error: \(contactDB.lastErrorMessage())")
}
}
@IBAction func findContact(sender: AnyObject) {
let contactDB = FMDatabase(path: databasePath as String)
if contactDB.open() {
let querySQL = "SELECT address, phone FROM CONTACTS WHERE name = '\(name.text)'"
let results:FMResultSet? = contactDB.executeQuery(querySQL,
withArgumentsInArray: nil)
if results?.next() == true {
address.text = results?.stringForColumn("address")
phone.text = results?.stringForColumn("phone")
status.text = "Record Found"
} else {
status.text = "Record not found"
address.text = ""
phone.text = ""
}
contactDB.close()
} else {
print("Error: \(contactDB.lastErrorMessage())")
}
}
}
答案 0 :(得分:0)
您URLByAppendingPathComponent
返回NSURL
是正确的,但您始终可以使用absoluteString
将路径设为String
并继续使用该字符串,因为您已经做的:
let databaseURL = NSURL(fileURLWithPath:NSTemporaryDirectory()).URLByAppendingPathComponent("contacts.db")
let databasePath = databaseURL.absoluteString
或者您可以使用快捷方式直接构建databasePath而不使用NSURL
:
let databasePath = NSTemporaryDirectory() + "database.db"