应用启动时出现NSUnknownKeyException

时间:2015-11-10 05:29:39

标签: ios swift nsunknownkeyexception

程序启动,然后抛出一个错误,它将我带到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] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key name.'
*** First throw call stack:
(
    0   CoreFoundation                      0x0000000106aa8f45 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x0000000106522deb objc_exception_throw + 48
    2   CoreFoundation                      0x0000000106aa8b89 -[NSException raise] + 9
    3   Foundation                          0x000000010488fa6b -[NSObject(NSKeyValueCoding) setValue:forKey:] + 288
    4   UIKit                               0x0000000104e6e04c -[UIViewController setValue:forKey:] + 88
    5   UIKit                               0x000000010509ba71 -[UIRuntimeOutletConnection connect] + 109
    6   CoreFoundation                      0x00000001069e9a80 -[NSArray makeObjectsPerformSelector:] + 224
    7   UIKit                               0x000000010509a454 -[UINib instantiateWithOwner:options:] + 1864
    8   UIKit                               0x0000000104e74c16 -[UIViewController _loadViewFromNibNamed:bundle:] + 381
    9   UIKit                               0x0000000104e75542 -[UIViewController loadView] + 178
    10  UIKit                               0x0000000104e758a0 -[UIViewController loadViewIfRequired] + 138
    11  UIKit                               0x0000000104e76013 -[UIViewController view] + 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 workspaceDidEndTransaction:] + 188
    18  FrontBoardServices                  0x0000000108776784 -[FBSSerialQueue _performNext] + 192
    19  FrontBoardServices                  0x0000000108776af2 -[FBSSerialQueue _performNextFromRunLoopSource] + 45
    20  CoreFoundation                      0x00000001069d5011 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    21  CoreFoundation                      0x00000001069caf3c __CFRunLoopDoSources0 + 556
    22  CoreFoundation                      0x00000001069ca3f3 __CFRunLoopRun + 867
    23  CoreFoundation                      0x00000001069c9e08 CFRunLoopRunSpecific + 488
    24  UIKit                               0x0000000104cde4f5 -[UIApplication _run] + 402
    25  UIKit                               0x0000000104ce330d UIApplicationMain + 171
    26  SQLiteDatabaseExample               0x00000001047a05ad main + 109
    27  libdyld.dylib                       0x000000010824f92d start + 1
    28  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

1 个答案:

答案 0 :(得分:0)

检查您的ViewController的IBOutlets连接,并删除任何不需要的连接。当您删除班级中的插座代码时,可能会发生这种情况。

enter image description here

enter image description here