0

I am trying to make this simple app where user enters data in text fields and it displays those data in table view. I have multiple files to define the structure of this app. But when I run the app, it crashes with the following error:

Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key table.' *** First throw call stack: ( 0 CoreFoundation 0x000000011083934b exceptionPreprocess + 171 1 libobjc.A.dylib 0x000000010dc3921e objc_exception_throw + 48 2 CoreFoundation 0x0000000110839299 -[NSException raise] + 9 3 Foundation 0x000000010d74926f -[NSObject(NSKeyValueCoding) setValue:forKey:] + 291 4 UIKit 0x000000010e2a24ef -[UIViewController setValue:forKey:] + 88 5 UIKit 0x000000010e51679e -[UIRuntimeOutletConnection connect] + 109 6 CoreFoundation 0x00000001107de590 -[NSArray makeObjectsPerformSelector:] + 256 7 UIKit 0x000000010e515122 -[UINib instantiateWithOwner:options:] + 1867 8 UIKit 0x000000010e2a8c21 -[UIViewController _loadViewFromNibNamed:bundle:] + 386 9 UIKit 0x000000010e2a9543 -[UIViewController loadView] + 177 10 UIKit 0x000000010e5291ca -[UITableViewController loadView] + 84 11 UIKit 0x000000010e2a9878 -[UIViewController loadViewIfRequired] + 201 12 UIKit 0x000000010e2aa0cc -[UIViewController view] + 27 13 UIKit 0x000000010e173c51 -[UIWindow addRootViewControllerViewIfPossible] + 71 14 UIKit 0x000000010e1743a2 -[UIWindow _setHidden:forced:] + 293 15 UIKit 0x000000010e187cb5 -[UIWindow makeKeyAndVisible] + 42 16 UIKit 0x000000010e100c89 -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 4818 17 UIKit 0x000000010e106de9 -[UIApplication _runWithMainScene:transitionContext:completion:] + 1731 18 UIKit 0x000000010e103f69 -[UIApplication workspaceDidEndTransaction:] + 188 19 FrontBoardServices 0x0000000111ef7723 __FBSSERIALQUEUE_IS_CALLING_OUT_TO_A_BLOCK + 24 20 FrontBoardServices 0x0000000111ef759c -[FBSSerialQueue _performNext] + 189 21 FrontBoardServices 0x0000000111ef7925 -[FBSSerialQueue _performNextFromRunLoopSource] + 45 22 CoreFoundation 0x00000001107de311 CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION + 17 23 CoreFoundation 0x00000001107c359c __CFRunLoopDoSources0 + 556 24 CoreFoundation 0x00000001107c2a86 __CFRunLoopRun + 918 25 CoreFoundation 0x00000001107c2494 CFRunLoopRunSpecific + 420 26 UIKit 0x000000010e1027e6 -[UIApplication _run] + 434 27 UIKit 0x000000010e108964 UIApplicationMain + 159 28 Morning Star 2 0x000000010d6531bf main + 111 29 libdyld.dylib 0x000000011176668d start + 1 ) libc++abi.dylib: terminating with uncaught exception of type NSException

This is my EventBrain.swift File:

import Foundation

struct Event{

    var title:String?
    var location:String?

    init(tits: String, locs: String){
        self.title = tits
        self.location = locs
    }
}

This is my viewController.swift file:

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var txtTitle: UITextField!

    @IBOutlet weak var txtLocation: UITextField!

    @IBOutlet weak var txtDate: UITextField!

    @IBOutlet weak var txtTime: UITextField!

    var eventsArray = [Event]()

    @IBAction func btnSave() {

        let event = Event(tits: txtTitle.text!, locs: txtLocation.text!)

        eventsArray.append(event)

    }
}

This is my EventsTable.swift file:

import UIKit

class EventsTable: UITableViewController {

    var tableData = ViewController()

    // MARK: - Table view data source

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return tableData.eventsArray.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        //let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)

        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CustomeCell

        cell.title.text = tableData.eventsArray[indexPath.row].title

        cell.location.text = tableData.eventsArray[indexPath.row].location

        return cell
    }
}

This is my customeCell.swift file:

import UIKit

class CustomeCell: UITableViewCell {

    @IBOutlet weak var title: UILabel!

    @IBOutlet weak var location: UILabel!

}

And finally here is my main.Storyboard:

Main.Storyboard

Cœur
  • 37,241
  • 25
  • 195
  • 267
Kunj Patel
  • 75
  • 3
  • 11

1 Answers1

4

Usually this happens when you connect an IBOutlet in InterfaceBuilder, then delete the code that defines the outlet.

The problem is that the Storyboard/XIB file tries to use KVC (Key Value Coding) to reference the property of the view controller for the outlet, but it's no longer there, so the KVC call crashes.

Look through your storyboards/xibs for a scene/view controller that has an outlet "table" that no longer exists.

Duncan C
  • 128,072
  • 22
  • 173
  • 272