[swift] How to fix Error: this class is not key value coding-compliant for the key tableView.'

I made an app with Table View and Segmented Control, and this is my first time. I'm using some code and some tutorials, but It's not working. When I run my app It's crashing and it's showing this Error in logs:

MyApplication[4928:336085] * Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key tableView.' * First throw call stack: ( 0 CoreFoundation 0x000000010516fd85 __exceptionPreprocess + 165 1 libobjc.A.dylib 0x0000000105504deb objc_exception_throw + 48 2 CoreFoundation 0x000000010516f9c9 -[NSException raise] + 9 3 Foundation 0x000000010364e19b -[NSObject(NSKeyValueCoding) setValue:forKey:] + 288 4 UIKit 0x0000000103c37d0c -[UIViewController setValue:forKey:] + 88 5 UIKit 0x0000000103e6e7fb -[UIRuntimeOutletConnection connect] + 109 6 CoreFoundation 0x00000001050a9890 -[NSArray makeObjectsPerformSelector:] + 224 7 UIKit 0x0000000103e6d1de -[UINib instantiateWithOwner:options:] + 1864 8 UIKit 0x0000000103c3e8d6 -[UIViewController _loadViewFromNibNamed:bundle:] + 381 9 UIKit 0x0000000103c3f202 -[UIViewController loadView] + 178 10 UIKit 0x0000000103c3f560 -[UIViewController loadViewIfRequired] + 138 11 UIKit 0x0000000103c3fcd3 -[UIViewController view] + 27 12 UIKit 0x000000010440b024 -[_UIFullscreenPresentationController _setPresentedViewController:] + 87 13 UIKit 0x0000000103c0f5ca -[UIPresentationController initWithPresentedViewController:presentingViewController:] + 133 14 UIKit 0x0000000103c525bb -[UIViewController _presentViewController:withAnimationController:completion:] + 4002 15 UIKit 0x0000000103c5585c -[UIViewController _performCoordinatedPresentOrDismiss:animated:] + 489 16 UIKit 0x0000000103c5536b -[UIViewController presentViewController:animated:completion:] + 179 17 UIKit 0x00000001041feb8d __67-[UIStoryboardModalSegueTemplate newDefaultPerformHandlerForSegue:]_block_invoke + 243 18 UIKit 0x00000001041ec630 -[UIStoryboardSegueTemplate _performWithDestinationViewController:sender:] + 460 19 UIKit 0x00000001041ec433 -[UIStoryboardSegueTemplate _perform:] + 82 20 UIKit 0x00000001041ec6f7 -[UIStoryboardSegueTemplate perform:] + 156 21 UIKit 0x0000000103aa6a8d -[UIApplication sendAction:to:from:forEvent:] + 92 22 UIKit 0x0000000103c19e67 -[UIControl sendAction:to:forEvent:] + 67 23 UIKit 0x0000000103c1a143 -[UIControl _sendActionsForEvents:withEvent:] + 327 24 UIKit 0x0000000103c19263 -[UIControl touchesEnded:withEvent:] + 601 25 UIKit 0x0000000103b1999f -[UIWindow _sendTouchesForEvent:] + 835 26 UIKit 0x0000000103b1a6d4 -[UIWindow sendEvent:] + 865 27 UIKit 0x0000000103ac5dc6 -[UIApplication sendEvent:] + 263 28 UIKit 0x0000000103a9f553 _UIApplicationHandleEventQueue + 6660 29 CoreFoundation 0x0000000105095301 _CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION_ + 17 30 CoreFoundation 0x000000010508b22c __CFRunLoopDoSources0 + 556 31 CoreFoundation 0x000000010508a6e3 __CFRunLoopRun + 867 32 CoreFoundation 0x000000010508a0f8 CFRunLoopRunSpecific + 488 33 GraphicsServices 0x000000010726dad2 GSEventRunModal + 161 34 UIKit 0x0000000103aa4f09 UIApplicationMain + 171 35 Dhikr 0x0000000101f26282 main + 114 36 libdyld.dylib 0x00000001064c392d start + 1 ) libc++abi.dylib: terminating with uncaught exception of type NSException

The code that I used is:

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

let foodList:[String] = ["Bread", "Meat", "Pizza", "Other"]
let drinkList:[String] = ["Water", "Soda", "Juice", "Other"]

@IBOutlet weak var mySegmentedControl: UISegmentedControl!
@IBOutlet weak var myTableView: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    var returnValue = 0

    switch(mySegmentedControl.selectedSegmentIndex) {
    case 0:
        returnValue = foodList.count
        break
    case 1:
        returnValue = drinkList.count
        break
    default:
        break
    }

    return returnValue
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let myCell = tableView.dequeueReusableCellWithIdentifier("myCells", forIndexPath: indexPath)

    switch(mySegmentedControl.selectedSegmentIndex) {
    case 0:
        myCell.textLabel!.text = foodList[indexPath.row]
        break
    case 1:
        myCell.textLabel!.text = drinkList[indexPath.row]
        break
    default:
        break
    }

    return myCell
}

@IBAction func segmentedControlActionChanged(sender: AnyObject) {
    myTableView.reloadData()  
}

Here is main.Storyboard

enter image description here

I checked the code many times, but it's not working. First I had to use only Table View, watching this tutorial (https://www.youtube.com/watch?v=ABVLSF3Vqdg) I thought it will work to use Segmented Control as in tutorial. But still doesn't work. Same code, same error. Can someone help me ?

This question is related to swift uitableview uisegmentedcontrol

The answer is


You have your storyboard set up to expect an outlet called tableView but the actual outlet name is myTableView.

If you delete the connection in the storyboard and reconnect to the right variable name, it should fix the problem.


Any chance that you changed the name of your table view from "tableView" to "myTableView" at some point?


Questions with swift tag:

Make a VStack fill the width of the screen in SwiftUI Xcode 10.2.1 Command PhaseScriptExecution failed with a nonzero exit code Command CompileSwift failed with a nonzero exit code in Xcode 10 Convert Json string to Json object in Swift 4 iOS Swift - Get the Current Local Time and Date Timestamp Xcode 9 Swift Language Version (SWIFT_VERSION) How do I use Safe Area Layout programmatically? How can I use String substring in Swift 4? 'substring(to:)' is deprecated: Please use String slicing subscript with a 'partial range from' operator Safe Area of Xcode 9 The use of Swift 3 @objc inference in Swift 4 mode is deprecated? Swift error : signal SIGABRT how to solve it How to update the constant height constraint of a UIView programmatically? Convert NSDate to String in iOS Swift Waiting until the task finishes Type of expression is ambiguous without more context Swift Removing object from array in Swift 3 Date to milliseconds and back to date in Swift How does String substring work in Swift How does String.Index work in Swift Request Permission for Camera and Library in iOS 10 - Info.plist How to use addTarget method in swift 3 swift 3.0 Data to String? how to open an URL in Swift3 Get current date in Swift 3? Updating to latest version of CocoaPods? Swift programmatically navigate to another view controller/scene Correctly Parsing JSON in Swift 3 What is the 'open' keyword in Swift? Swift - How to detect orientation changes How to set Status Bar Style in Swift 3 how to make UITextView height dynamic according to text length? How to program a delay in Swift 3 How to set UICollectionViewCell Width and Height programmatically How can I mimic the bottom sheet from the Maps app? How do I write dispatch_after GCD in Swift 3, 4, and 5? How do I dispatch_sync, dispatch_async, dispatch_after, etc in Swift 3, Swift 4, and beyond? Default optional parameter in Swift function Swift: Display HTML data in a label or textView How to capture multiple repeated groups? How to pass data using NotificationCenter in swift 3.0 and NSNotificationCenter in swift 2.0? Convert string to date in Swift How to fix Error: this class is not key value coding-compliant for the key tableView.' Basic example for sharing text or image with UIActivityViewController in Swift Date Format in Swift How to display .svg image using swift How do I make a new line in swift 'Linker command failed with exit code 1' when using Google Analytics via CocoaPods Swift Error: Editor placeholder in source file Round up double to 2 decimal places How to make a UILabel clickable?

Questions with uitableview tag:

Adding a UISegmentedControl to UITableView make UITableViewCell selectable only while editing How to fix Error: this class is not key value coding-compliant for the key tableView.' UITableView example for Swift Swift - how to make custom header for UITableView? How to insert new cell into UITableView in Swift How to detect tableView cell touched or clicked in swift Dynamic Height Issue for UITableView Cells (Swift) UIButton action in table view cell How to get the indexpath.row when an element is activated? Refresh certain row of UITableView based on Int in Swift Class has no initializers Swift UITableViewCell Selected Background Color on Multiple Selection How to get textLabel of selected row in swift? iOS 8 UITableView separator inset 0 not working swift UITableView set rowHeight Custom UITableViewCell from nib in Swift creating custom tableview cells in swift self.tableView.reloadData() not working in Swift Add swipe to delete UITableViewCell Set UITableView content inset permanently Get button click inside UITableViewCell UITableView load more when scrolling to bottom like Facebook application Changing Font Size For UITableView Section Headers Custom edit view in UITableViewCell while swipe left. Objective-C or Swift How to hide first section header in UITableView (grouped style) Remove empty space before cells in UITableView Why is there extra padding at the top of my UITableView with style UITableViewStyleGrouped in iOS7 How to fix UITableView separator on iOS 7? Using Auto Layout in UITableView for dynamic cell layouts & variable row heights How to resize superview to fit all subviews with autolayout? UITableView with fixed section headers Swipe to Delete and the "More" button (like in Mail app on iOS 7) Async image loading from url inside a UITableView cell - image changes to wrong image while scrolling How to tell when UITableView has completed ReloadData? Creating a UITableView Programmatically Customize UITableView header section How to remove empty cells in UITableView? Change UITableView height dynamically Assertion failure in dequeueReusableCellWithIdentifier:forIndexPath: How to set the UITableView Section title programmatically (iPhone/iPad)? How to Implement Custom Table View Section Headers and Footers with Storyboard Custom Cell Row Height setting in storyboard is not responding Hide separator line on one UITableViewCell UITableView set to static cells. Is it possible to hide some of the cells programmatically? How does cellForRowAtIndexPath work? How to pass prepareForSegue: an object How to disable scrolling in UITableView table when the content fits on the screen How to add spacing between UITableViewCell add image to uitableview cell

Questions with uisegmentedcontrol tag:

Adding a UISegmentedControl to UITableView How to fix Error: this class is not key value coding-compliant for the key tableView.' Color Tint UIButton Image Change font size of UISegmentedControl