Keep getting an error/crash due to uncaught exception 'NSUnknownKeyException' [duplicate] - ios

This question already has answers here:
Xcode - How to fix 'NSUnknownKeyException', reason: … this class is not key value coding-compliant for the key X" error?
(79 answers)
Closed 5 years ago.
I've checked my connections and class names as all the other threads with this same issue suggest.
Error message
*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key parksTableView.'
View controller where I get the crash
import UIKit
class ParksViewController: UITableView, UITableViewDataSource, UITableViewDelegate {
var parkNames = ["Sunnyside", "South Oak", "Tower", "Arno", "Arbor", "Holmes", "Brookside", "Brookside Tennis Courts", "Loose", "Gilham", "Brush Creek", "Westwood"]
#IBOutlet weak var textLabel: UILabel!
#IBOutlet weak var parksTableView: UITableView!
func viewDidLoad(){
self.parksTableView.delegate = self
self.parksTableView.dataSource = self
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return parkNames.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
print(indexPath.row)
cell.textLabel?.text = parkNames[indexPath.row]
return cell
}
Maybe I'm just not connecting the right things to the right places? I've tried a combination of different connections and have deleted and reconnected connections many times in order to try to fix this issue, but it I still can't understand what is wrong.
Storyboard
Connection Inspector of ParksViewController

If you look at the error, it says <UIViewController 0x7fc416a04f80>. You think you have a ParksViewController but you really just have a plain UIViewController.

Related

How do I fix this "Unknown Key Exception" error in Xcode?

I'm very new to coding so I really have no idea what's going on.
I've tried pasting the error into this website and looked around for people's responses but they either slightly altered from my error code or their explanation was too convoluted for me.
Code:
import UIKit
class BasicsListScreen: UIViewController {
#IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
}
}
extension BasicsListScreen: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 70
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return basics.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as? TableViewCell
cell?.verbLabel.text = basics[indexPath.row]
return cell!
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let vc = storyboard?.instantiateViewController(withIdentifier: "ConjViewController") as? ConjViewController
vc?.pastMe = mePast[indexPath.row]
vc?.pastYou = youPast[indexPath.row]
vc?.pastHe = hePast[indexPath.row]
vc?.pastShe = shePast[indexPath.row]
vc?.pastWe = wePast[indexPath.row]
vc?.pastYall = yallPast[indexPath.row]
vc?.pastThey = theyPast[indexPath.row]
vc?.pastPassive = passivePast[indexPath.row]
vc?.presentMe = mePresent[indexPath.row]
vc?.presentYou = youPresent[indexPath.row]
vc?.presentHe = hePresent[indexPath.row]
vc?.presentShe = shePresent[indexPath.row]
vc?.presentWe = wePresent[indexPath.row]
vc?.presentYall = yallPresent[indexPath.row]
vc?.presentThey = theyPresent[indexPath.row]
vc?.presentPassive = passivePresent[indexPath.row]
self.navigationController?.pushViewController(vc!, animated: true)
}
}
28784:1039713] Unknown class BasicsListScreen in Interface Builder
file. 2019-07-10 13:30:01.795516+0100 App[28784:1039713] *
Terminating app due to uncaught exception 'NSUnknownKeyException',
reason: '[< UIViewController 0x7fcc87619000>
setValue:forUndefinedKey:]: this class is not key value
coding-compliant for the key tableView.'
* First throw call stack: (
Attach your tableView IBOutlet in code to your UITableView in Interface Builder.
Start Developing iOS Apps (Swift): Connect the UI to Code
Take a look at this page Terminating app due to uncaught exception 'NSUnknownKeyException' : iOS app crash. There may be a broken connection to a referenced outlet. The page above shows you what that looks like.
Another thing to look at is to make sure your table view is connected to the right class as Daniel Storm said. Open your storyboard and click on the view that you think is connected BasicsListScreen. Open the "Inspector" by clicking the button in the far upper right of Xcode. Then click "Identity Inspector" which is the middle button. It should show BasicsListScreen in the "Custom Class". Make sure that is right.
The error message sounds like a broken connection in BasicMathLevelOne.xib. It is the result from KVC trying to set a value on your InheritController for the key "you" but the class has no KVC compliant accessor (any more?).
To find the exact spot where the error occurs set an exception breakpoint in Xcode (press Command-6, click the "+" in lower left corner, chose "Add Exception Breakpoint"). Running the app in the debugger should make it stop at the place where the error occurs.

SIGABRT error in dequeueReusableCell(withIdentifier:), when using custom UITableViewCell

I'm building an app with multiple scenes and a table view with custom cells in each. I got the home screen table view to work fine and then I segue to the new scene from the custom cells. When it segues, my second view controller crashes.
Here is my code for the view controller
import UIKit
class QuestionViewController: UIViewController {
#IBOutlet weak var questionLabel: UILabel!
#IBOutlet weak var submitButton: UIButton!
#IBOutlet weak var qTableView: UITableView!
var answers : [QuestionOption] = []
override func viewDidLoad() {
super.viewDidLoad()
answers = [QuestionOption(text: "test"), QuestionOption(text: "test"), QuestionOption(text: "test"), QuestionOption(text: "test")]
qTableView.delegate = self
qTableView.dataSource = self
submitButton.setTitle("Submit", for: .normal)
questionLabel.text = "test question"
}
}
extension QuestionViewController: UITableViewDataSource, UITableViewDelegate{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return answers.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let a = answers[indexPath.row]
let cell = qTableView.dequeueReusableCell(withIdentifier: "QuestionOptionCell") as! QuestionOptionCell
cell.setOption(option: a)
return cell
}
}
Here's my code for the cell
import UIKit
class QuestionOptionCell: UITableViewCell {
#IBOutlet weak var cellTitle: UILabel!
func setOption(option: QuestionOption){
cellTitle.text = option.text
}
}
Here's my code for the QuestionOption class
import Foundation
import UIKit
class QuestionOption{
var text: String
init(text: String){
self.text = text
}
}
Crash log
2019-02-20 14:33:28.394695-0800 iQuiz[8935:822409] *** NSForwarding: warning: object 0x7fd608407c40 of class 'iQuiz.QuestionOption' does not implement methodSignatureForSelector: -- trouble ahead
Unrecognized selector -[iQuiz.QuestionOption initWithCoder:]
2019-02-20 14:33:28.395281-0800 iQuiz[8935:822409] Unrecognized selector -[iQuiz.QuestionOption initWithCoder:]
Here's my storyboard if that helps at all
I've made sure my identifier matches and I don't have any extraneous or unconnected outlets, those are the only solution to this problem I can find online.
The crash log says that QuestionOption must be a subclass of NSObject and adopt NSCoding which is overkill in this case. Actually a struct would be sufficient.
You can avoid it by deleting the method in QuestionOptionCell
func setOption(option: QuestionOption){
cellTitle.text = option.text
}
and set the value in cellForRowAt directly by replacing
cell.setOption(option: a)
with
cell.cellTitle.text = a.text
Things to check:
Verify that "QuestionOptionCell" is indeed the reuse identifier for the cell.
Verify that the selected type for the cell is QuestionOptionCell.
In cellForRowAt, use tableView.dequeueReusableCell instead of qTableView.dequeueReusableCell.
Otherwise, share the crash log with us.

How to proper create a custom cell with a xib file, register, and load it ?

I want
to create a custom table cell and add all the components and register it
I did
I created these files below :
PlaceDetailCell.xib
with the id of customPlaceDetailCell
CustomPlaceDetailCell.swift
import UIKit
class CustomPlaceDetailCell: UITableViewCell {
#IBOutlet weak var country: UILabel!
#IBOutlet weak var address: UILabel!
#IBOutlet weak var distance: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
I register
//register xib file
placesTable.register(UINib(nibName: "PlaceDetailCell", bundle: nil), forCellReuseIdentifier: "customPlaceDetailCell")
This is my cellForRowAt
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "customPlaceDetailCell", for: indexPath)
as! CustomPlaceDetailCell
if places[indexPath.row]["name"] != nil {
cell.textLabel?.text = places[indexPath.row]["name"]! + " (" + distances[indexPath.row] + " miles)"
}
return cell
}
I got
a crash when I click on that cell even if the app is successfully Build.
Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key address.'
*** First throw call stack:
I hope
someone can provide some guidance for me to do the right things.
You need to set the xib file custom class to the class you cast it to
let cell = tableView.dequeueReusableCell(withIdentifier: "customPlaceDetailCell", for: indexPath)
as! CustomPlaceDetailCell
as it seems that xib name is PlaceDetailCell with class PlaceDetailCell and you cast it to CustomPlaceDetailCell
if you register this
placesTable.register(UINib(nibName: "PlaceDetailCell", bundle: nil), forCellReuseIdentifier: "customPlaceDetailCell")
then the xib custom class should be set to CustomPlaceDetailCell not to PlaceDetailCell which is expected as always the xib name = it's class name that you create and then wanted to change the class name

Unrecognised selector error for a UITableView in TabBarController

My app use MMDrawerControllerin order to implement a left drawer which handle a simple menu. A entry of this menu refers to a UITabController which I instantiate in this way.
mainWindowController = self.storyboard?.instantiateViewControllerWithIdentifier("UITabBarController") as! UITabBarController
Now, the first UIViewController used by the TabController has a table view which uses a custom UIViewCell. The class for the custom cell is this:
class NewsCell: UITableViewCell {
#IBOutlet weak var title: UILabel!
#IBOutlet weak var date: UILabel!
#IBOutlet weak var newsText: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}
I checked that the outlets are properly connected with the correct storybook element. However, my app crash at this instruction,
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("NewsCell", forIndexPath: indexPath) as! NewsCell
with the following error: exception 'NSInvalidArgumentException', reason: '-[UILabel isEqualToString:]: unrecognized selector sent to instance 0x7ff4d362a6f0'.
It seems like I have no set the proper identifier in my inspector; however, obviously, I have done it. The situation of my storyboard is this:
Do anyone have any suggestions which can explain this problem? I hope I made myself clear.
Somewhere you are using isEqualToString Method and instead of passing string to that method,UILabel is passing. Please check. Otherwise put that code, I'll check and let you know.

performSegueWithIdentifier doesn't work - Swift

Im trying to use performSegueWithIdentifier when user taps in a row of a table view. How ever, it doesn't work and i have the next error:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not perform segue with identifier 'categoriesS'. A segue must either have a performHandler or it must override -perform.'
I googled the error, but i don't find anything. I've already create the push segues in the storyboard and set them the identifiers that I use in my code.
This is my code(PD: Im using this tableview, as a side bar menu):
import Foundation
class menuVC: UIViewController,UITableViewDelegate, UITableViewDataSource {
#IBOutlet weak var ProfileImage: UIImageView!
#IBOutlet weak var myTable: UITableView!
var opciones = [String]()
var segues = [String]()
override func viewDidLoad() {
super.viewDidLoad()
myTable.delegate = self
opciones = ["Categories","My Coins","Get Coins","Share","LogOff","Exit"]
segues = ["categoriesS","myCoinsS","getCoinsS"]
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 6
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
print(opciones[indexPath.row])
self.performSegueWithIdentifier(segues[indexPath.row], sender: self)
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell
cell.textLabel?.text = opciones[indexPath.row]
return cell
}
}
Thanks!
You have given a wrong segue as console clearly stated that. You are looking for 'categoriesS' which is in 'segue' array, but you are passing from 'opciones' array. Look closely.
Edit
I saw your edit, looks like u have populate your table using different array, but u have used other array in didselectrow.please check it

Resources