应用程序崩溃,因为 - "Class is not key value coding-compliant for the key"
App crashes because - "Class is not key value coding-compliant for the key"
我的应用程序每次进入 VieController3 都会崩溃,我不知道为什么/如何解决这个问题。
http://img3.fotos-hochladen.net/uploads/bildschirmfoto6cji4t9fp1.png
由于未捕获的异常 'NSUnknownKeyException',正在终止应用程序,原因:'[setValue:forUndefinedKey:]:此 class 与键的键值编码不兼容tableView.'
import UIKit
import CoreData
class ViewController3: UIViewController, UITableViewDataSource {
var people = [NSManagedObject]()
@IBOutlet weak var tableView: UITableView!
func tableView(tableView: UITableView,
numberOfRowsInSection section: Int) -> Int {
return people.count
}
func tableView(tableView: UITableView,
cellForRowAtIndexPath
indexPath: NSIndexPath) -> UITableViewCell {
let cell =
tableView.dequeueReusableCellWithIdentifier("Cell")
as UITableViewCell
let person = people[indexPath.row]
cell.textLabel!.text = person.valueForKey("name") as String?
return cell
}
@IBAction func addExercise(sender: AnyObject) {
var alert = UIAlertController(title: "New exercise",
message: "Add a new exercise",
preferredStyle: .Alert)
let saveAction = UIAlertAction(title: "Save",
style: .Default) { (action: UIAlertAction!) -> Void in
let textField = alert.textFields![0] as UITextField
self.saveName(textField.text)
self.tableView.reloadData()
}
let cancelAction = UIAlertAction(title: "Cancel",
style: .Default) { (action: UIAlertAction!) -> Void in
}
alert.addTextFieldWithConfigurationHandler {
(textField: UITextField!) -> Void in
}
alert.addAction(saveAction)
alert.addAction(cancelAction)
presentViewController(alert,
animated: true,
completion: nil)
}
func saveName(name: String) {
//1
let appDelegate =
UIApplication.sharedApplication().delegate as AppDelegate
let managedContext = appDelegate.managedObjectContext!
//2
let entity = NSEntityDescription.entityForName("Person",
inManagedObjectContext:
managedContext)
let person = NSManagedObject(entity: entity!,
insertIntoManagedObjectContext:managedContext)
//3
person.setValue(name, forKey: "name")
//4
var error: NSError?
if !managedContext.save(&error) {
println("Could not save \(error), \(error?.userInfo)")
}
//5
people.append(person)
}
override func viewDidLoad() {
super.viewDidLoad()
title = "\"Edit your Exercises\""
tableView.registerClass(UITableViewCell.self,
forCellReuseIdentifier: "Cell")
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
//1
let appDelegate =
UIApplication.sharedApplication().delegate as AppDelegate
let managedContext = appDelegate.managedObjectContext!
//2
let fetchRequest = NSFetchRequest(entityName:"Person")
//3
var error: NSError?
let fetchedResults =
managedContext.executeFetchRequest(fetchRequest,
error: &error) as [NSManagedObject]?
if let results = fetchedResults {
people = results
} else {
println("Could not fetch \(error), \(error!.userInfo)")
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
最新的截图显示了之前的另一个错误,这可能是导致下面错误的原因。
Unknown class ViewController3 in InterfaceBuilder
对我来说,这表明您曾尝试在 ViewController 上设置 Class,但输入的 class 不正确。您是否创建了继承 UIViewController
的 class ViewController3
?
因此,ViewController 默认为默认 UIViewController
,而 UIViewController
没有 属性 tableView
,因此它正在崩溃。
希望对您有所帮助。
我的应用程序每次进入 VieController3 都会崩溃,我不知道为什么/如何解决这个问题。
http://img3.fotos-hochladen.net/uploads/bildschirmfoto6cji4t9fp1.png
由于未捕获的异常 'NSUnknownKeyException',正在终止应用程序,原因:'[setValue:forUndefinedKey:]:此 class 与键的键值编码不兼容tableView.'
import UIKit
import CoreData
class ViewController3: UIViewController, UITableViewDataSource {
var people = [NSManagedObject]()
@IBOutlet weak var tableView: UITableView!
func tableView(tableView: UITableView,
numberOfRowsInSection section: Int) -> Int {
return people.count
}
func tableView(tableView: UITableView,
cellForRowAtIndexPath
indexPath: NSIndexPath) -> UITableViewCell {
let cell =
tableView.dequeueReusableCellWithIdentifier("Cell")
as UITableViewCell
let person = people[indexPath.row]
cell.textLabel!.text = person.valueForKey("name") as String?
return cell
}
@IBAction func addExercise(sender: AnyObject) {
var alert = UIAlertController(title: "New exercise",
message: "Add a new exercise",
preferredStyle: .Alert)
let saveAction = UIAlertAction(title: "Save",
style: .Default) { (action: UIAlertAction!) -> Void in
let textField = alert.textFields![0] as UITextField
self.saveName(textField.text)
self.tableView.reloadData()
}
let cancelAction = UIAlertAction(title: "Cancel",
style: .Default) { (action: UIAlertAction!) -> Void in
}
alert.addTextFieldWithConfigurationHandler {
(textField: UITextField!) -> Void in
}
alert.addAction(saveAction)
alert.addAction(cancelAction)
presentViewController(alert,
animated: true,
completion: nil)
}
func saveName(name: String) {
//1
let appDelegate =
UIApplication.sharedApplication().delegate as AppDelegate
let managedContext = appDelegate.managedObjectContext!
//2
let entity = NSEntityDescription.entityForName("Person",
inManagedObjectContext:
managedContext)
let person = NSManagedObject(entity: entity!,
insertIntoManagedObjectContext:managedContext)
//3
person.setValue(name, forKey: "name")
//4
var error: NSError?
if !managedContext.save(&error) {
println("Could not save \(error), \(error?.userInfo)")
}
//5
people.append(person)
}
override func viewDidLoad() {
super.viewDidLoad()
title = "\"Edit your Exercises\""
tableView.registerClass(UITableViewCell.self,
forCellReuseIdentifier: "Cell")
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
//1
let appDelegate =
UIApplication.sharedApplication().delegate as AppDelegate
let managedContext = appDelegate.managedObjectContext!
//2
let fetchRequest = NSFetchRequest(entityName:"Person")
//3
var error: NSError?
let fetchedResults =
managedContext.executeFetchRequest(fetchRequest,
error: &error) as [NSManagedObject]?
if let results = fetchedResults {
people = results
} else {
println("Could not fetch \(error), \(error!.userInfo)")
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
最新的截图显示了之前的另一个错误,这可能是导致下面错误的原因。
Unknown class ViewController3 in InterfaceBuilder
对我来说,这表明您曾尝试在 ViewController 上设置 Class,但输入的 class 不正确。您是否创建了继承 UIViewController
的 class ViewController3
?
因此,ViewController 默认为默认 UIViewController
,而 UIViewController
没有 属性 tableView
,因此它正在崩溃。
希望对您有所帮助。