程序启动,然后抛出一个错误,它把我带到 appDelegate.swift 文件 --class AppDelegate: UIResponder, UIApplicationDelegate { -- thread 1:signal sigabort
这是代码:
import UIKit
class ViewController: UIViewController {
let filemgr = NSFileManager.defaultManager()
let dirPaths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
@IBOutlet weak var address: UITextField!
@IBOutlet weak var street: UITextField!
@IBOutlet weak var city: UITextField!
@IBOutlet weak var state: UITextField!
@IBOutlet weak var zip: UITextField!
@IBOutlet weak var status: UILabel!
var database = NSString() // <-- CRASHING HERE
override func viewDidLoad() {
super.viewDidLoad()
let docsDir = dirPaths[0]
let databasePath = docsDir.stringByAppendingString("/contacts.db") // This seems to work
if !filemgr.fileExistsAtPath(databasePath as String) {
let contactDB = FMDatabase(path: databasePath as String)
if contactDB == nil {
print("Error DB")
}
if contactDB.open() {
let sql = "CREATE TABLE IF NOT EXISTS Houses (ID INTEGER PRIMARY KEY AUTOINCREMENT, Address TEXT, Street TEXT, City TEXT, State TEXT, Zip TEXT)"
if !contactDB.executeStatements(sql) {
print("Error DB")
}
else {
print("Error DB")
}
}
}
}
@IBAction func saveData(sender: AnyObject) {
let docsDir = dirPaths[0]
let databasePath = docsDir.stringByAppendingString("/contacts.db") // This seems to work
if !filemgr.fileExistsAtPath(databasePath as String) {
let contactDB = FMDatabase(path: databasePath as String)
if contactDB.open() {
let sql = "insert into Houses (Address, State, City, State, Zip) values ('\(address),\(street),\(city),\(state),\(zip))"
let result = contactDB.executeUpdate(sql, withArgumentsInArray: nil)
if !result {
status.text = "Failed to add house"
print("DB Error")
}
else {
status.text = "Added House"
address.text = ""
street.text = ""
city.text = ""
state.text = ""
zip.text = ""
}
}
else {
print("DB Error")
}
}
}
@IBAction func findContact(sender: AnyObject) {
let docsDir = dirPaths[0]
let databasePath = docsDir.stringByAppendingString("/contacts.db")
if !filemgr.fileExistsAtPath(databasePath as String) {
let contactDB = FMDatabase(path: databasePath as String)
if contactDB.open() {
let sql = "SELECT address, street, state, state, zip WHERE ID = 1" // Change this later
let results:FMResultSet? = contactDB.executeQuery(sql,withArgumentsInArray: nil)
if results?.next() == true {
address.text = results?.stringForColumn("Address")
street.text = results?.stringForColumn("Street")
city.text = results?.stringForColumn("City")
state.text = results?.stringForColumn("State")
zip.text = results?.stringForColumn("Zip")
}
else {
status.text = "Record not found"
address.text = ""
street.text = ""
city.text = ""
state.text = ""
zip.text = ""
}
contactDB.close()
}
else {
print("DBError")
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
以下是错误:
2015-11-10 00:34:33.385 SQLiteDatabaseExample[3656:218507] *** 由于未捕获的异常“NSUnknownKeyException”而终止应用程序,原因:“[setValue:forUndefinedKey:]:此类不符合键值编码键名。 *** 首先抛出调用堆栈: ( 0 核心基础 0x0000000106aa8f45 __exceptionPreprocess + 165 1 libobjc.A.dylib 0x0000000106522deb objc_exception_throw + 48 2 核心基础 0x0000000106aa8b89 -[NSException raise] + 9 3 基础 0x000000010488fa6b -[NSObject(NSKeyValueCoding) setValue:forKey:] + 288 4 UIKit 0x0000000104e6e04c-[UIViewController setValue:forKey:] + 88 5 UIKit 0x000000010509ba71-[UIRuntimeOutletConnection 连接] + 109 6 核心基础 0x00000001069e9a80 -[NSArray makeObjectsPerformSelector:] + 224 7 UIKit 0x000000010509a454-[UINib 实例化WithOwner:选项:] + 1864 8 UIKit 0x0000000104e74c16-[UIViewController _loadViewFromNibNamed:bundle:] + 381 9 UIKit 0x0000000104e75542-[UIViewController loadView] + 178 10 UIKit 0x0000000104e758a0-[UIViewController loadViewIfRequired] + 138 11 UIKit 0x0000000104e76013 -[UIViewController 视图] + 27 12 UIKit 0x0000000104d4f51c -[UIWindow addRootViewControllerViewIfPossible] + 61 13 UIKit 0x0000000104d4fc05-[UIWindow _setHidden:forced:] + 282 14 UIKit 0x0000000104d614a5 -[UIWindow makeKeyAndVisible] + 42 15 UIKit 0x0000000104cdb396-[UIApplication_callInitializationDelegatesForMainScene:transitionContext:] + 4131 16 UIKit 0x0000000104ce19c3-[UIApplication_runWithMainScene:transitionContext:completion:] + 1750 17 UIKit 0x0000000104cdeba3-[UIApplication 工作区DidEndTransaction:] + 188 18 前板服务 0x0000000108776784 -[FBSSerialQueue _performNext] + 192 19 前板服务 0x0000000108776af2 -[FBSSerialQueue _performNextFromRunLoopSource] + 45 20 核心基础 0x00000001069d5011 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17 21 核心基础 0x00000001069caf3c __CFRunLoopDoSources0 + 556 22 核心基础 0x00000001069ca3f3 __CFRunLoopRun + 867 23 核心基础 0x00000001069c9e08 CFRunLoopRunSpecific + 488 24 UIKit 0x0000000104cde4f5-[UIApplication_run] + 402 25 UIKit 0x0000000104ce330d UIApplicationMain + 171 26 SQLiteDatabaseExample 0x00000001047a05ad main + 109 27 libdyld.dylib 0x000000010824f92d 开始 + 1 28 ???0x0000000000000001 0x0 + 1 ) libc++abi.dylib:以 NSException 类型的未捕获异常终止 (lldb)