每次去VieController3时我的应用都崩溃了,我不知道为什么/如何解决这个问题。
http://img3.fotos-hochladen.net/uploads/bildschirmfoto6cji4t9fp1.png
由于未捕获的异常而终止应用程序' NSUnknownKeyException',原因:' [setValue:forUndefinedKey:]:此类与密钥tableView不符合键值编码。&# 39;
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.
}
}
答案 0 :(得分:0)
最新的屏幕截图显示之前的另一个错误,这可能会导致错误低于它。
Unknown class ViewController3 in InterfaceBuilder
对我来说,这表明您已尝试在ViewController上设置类,但该类输入错误。您是否创建了继承ViewController3
?
UIViewController
因此,ViewController默认为默认UIViewController
,而UIViewController
没有属性tableView
,因此它会崩溃。
希望有所帮助。