2015-11-05 181 views
0

我收到此錯誤,當我單擊表格單元格以傳輸到內部表格時。這個問題開始發生後,我複製代碼到另一個視圖控制器的效率,但撤消這一切後,我仍然得到錯誤未捕獲異常錯誤Swift 2.0

2015-11-05 21:52:07.505 Grades[11134:1292199] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<Grades.AssesmentViewController 0x7fb660743fe0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key addAssesment.' 
*** First throw call stack: 
(
    0 CoreFoundation      0x000000010623af45 __exceptionPreprocess + 165 
    1 libobjc.A.dylib      0x0000000107f5edeb objc_exception_throw + 48 
    2 CoreFoundation      0x000000010623ab89 -[NSException raise] + 9 
    3 Foundation       0x0000000106603a6b -[NSObject(NSKeyValueCoding) setValue:forKey:] + 288 
    4 UIKit        0x0000000106be204c -[UIViewController setValue:forKey:] + 88 
    5 UIKit        0x0000000106e0fa71 -[UIRuntimeOutletConnection connect] + 109 
    6 CoreFoundation      0x000000010617ba80 -[NSArray makeObjectsPerformSelector:] + 224 
    7 UIKit        0x0000000106e0e454 -[UINib instantiateWithOwner:options:] + 1864 
    8 UIKit        0x0000000107171730 -[UIStoryboard instantiateViewControllerWithIdentifier:] + 181 
    9 UIKit        0x000000010717634c -[UIStoryboardSegueTemplate instantiateOrFindDestinationViewControllerWithSender:] + 90 
    10 UIKit        0x00000001071765a9 -[UIStoryboardSegueTemplate _perform:] + 52 
    11 UIKit        0x000000010717688b -[UIStoryboardSegueTemplate perform:] + 156 
    12 UIKit        0x0000000106b979b1 -[UITableView _selectRowAtIndexPath:animated:scrollPosition:notifyDelegate:] + 1856 
    13 UIKit        0x0000000106b97c76 -[UITableView _userSelectRowAtPendingSelectionIndexPath:] + 388 
    14 UIKit        0x0000000106a631ba _runAfterCACommitDeferredBlocks + 317 
    15 UIKit        0x0000000106a76396 _cleanUpAfterCAFlushAndRunDeferredBlocks + 95 
    16 UIKit        0x0000000106a821c2 _afterCACommitHandler + 90 
    17 CoreFoundation      0x0000000106166947 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23 
    18 CoreFoundation      0x00000001061668b7 __CFRunLoopDoObservers + 391 
    19 CoreFoundation      0x000000010615c50b __CFRunLoopRun + 1147 
    20 CoreFoundation      0x000000010615be08 CFRunLoopRunSpecific + 488 
    21 GraphicsServices     0x000000010a857ad2 GSEventRunModal + 161 
    22 UIKit        0x0000000106a5730d UIApplicationMain + 171 
    23 Grades        0x0000000105cc4e1d main + 109 
    24 libdyld.dylib      0x0000000108a6f92d start + 1 
) 
libc++abi.dylib: terminating with uncaught exception of type NSException 
(lldb) 

這裏是我的第一個視圖控制器代碼:

import UIKit 
import CoreData 

class ViewController: UIViewController, UITableViewDataSource { 


    @IBOutlet weak var tableView: UITableView! 
    var subjects = [NSManagedObject]() 
    override func viewDidLoad() { 
     super.viewDidLoad() 

    } 

    func tableView(tableView: UITableView, 
     numberOfRowsInSection section: Int) -> Int { 
      return subjects.count 
    } 

    func tableView(tableView: UITableView, 
     cellForRowAtIndexPath 
     indexPath: NSIndexPath) -> UITableViewCell { 

      let cell = 
      tableView.dequeueReusableCellWithIdentifier("Cell") 

      let check = subjects[indexPath.row] 

      cell!.textLabel!.text = 
       check.valueForKey("name") as? String 

      return cell! 
    } 
    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 

    } 

    @IBAction func addName(sender: AnyObject) { 

     let alert = UIAlertController(title: "New Subject", message: "Add a new Subject", preferredStyle: .Alert) 
     let saveAction = UIAlertAction(title: "Save", 
      style: .Default, 
      handler: { (action:UIAlertAction) -> Void in 

       let textField = alert.textFields!.first 
       if textField != nil { 
        self.saveSubject(textField!.text!) 
        self.tableView.reloadData() 
       } 
       else { 

       } 
     }) 


     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 saveSubject(subject: String) { 

     let appDelegate = 
     UIApplication.sharedApplication().delegate as! AppDelegate 

     let managedContext = appDelegate.managedObjectContext 


     let entity = NSEntityDescription.entityForName("Subjects", 
      inManagedObjectContext:managedContext) 

     let check = NSManagedObject(entity: entity!, 
      insertIntoManagedObjectContext: managedContext) 


     check.setValue(subject, forKey: "name") 


     do { 
      try managedContext.save() 

      subjects.append(check) 
     } catch let error as NSError { 
      print("Could not save \(error), \(error.userInfo)") 
     } 
    } 
    override func viewWillAppear(animated: Bool) { 
     super.viewWillAppear(animated) 


     let appDelegate = 
     UIApplication.sharedApplication().delegate as! AppDelegate 

     let managedContext = appDelegate.managedObjectContext 


     let fetchRequest = NSFetchRequest(entityName: "Subjects") 


     do { 
      let results = 
      try managedContext.executeFetchRequest(fetchRequest) 
      subjects = results as! [NSManagedObject] 
     } catch let error as NSError { 
      print("Could not fetch \(error), \(error.userInfo)") 
     } 
     func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle,forRowAtIndexPath indexPath: NSIndexPath) { 

     } 
    } 

    func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) { 
     self.performSegueWithIdentifier("ShowAssesment", sender: self); 


    } 

} 

和我第二視圖控制器:

import UIKit 
import CoreData 

class AssesmentViewController: UITableViewController { 

    @IBOutlet weak var navTitle: UINavigationItem! 
    var assesments = [NSManagedObject]() 
    let cellIdentifier = "Cell" 
    var subjectSelected = String() 


    override func viewDidLoad() { 
     print(subjectSelected) 

    } 

    func assesmentForDisplay(atIndexPath indexPath: NSIndexPath){ 


    } 

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) 
     //cell.textLabel?.text = assesments[indexPath.row] 
     return cell 

    } 

    func changeNavTitle(newTitle: String) { 
     title = newTitle 
    } 

    @IBAction func addAssesment(sender: AnyObject) { 
     let alert = UIAlertController(title: "New Assesment", message: "Add a new Assesment", preferredStyle: .Alert) 
     let saveAction = UIAlertAction(title: "Save", 
      style: .Default, 
      handler: { (action:UIAlertAction) -> Void in 

       let textFieldName = alert.textFields!.first 

        self.saveAssesment(textFieldName!.text!) 
        self.tableView.reloadData() 


     }) 


     let cancelAction = UIAlertAction(title: "Cancel", 
      style: .Default) { (action: UIAlertAction) -> Void in 
     } 



     alert.addTextFieldWithConfigurationHandler { 
      (textField: UITextField) -> Void in 
      textField.placeholder = "Name" 
     } 
     alert.addAction(saveAction) 
     alert.addAction(cancelAction) 

     presentViewController(alert, 
      animated: true, 
      completion: nil) 

    } 

    func saveAssesment(subject: String) { 

     let appDelegate = 
     UIApplication.sharedApplication().delegate as! AppDelegate 

     let managedContext = appDelegate.managedObjectContext 


     let entity = NSEntityDescription.entityForName("Assesment", 
      inManagedObjectContext:managedContext) 

     let check = NSManagedObject(entity: entity!, 
      insertIntoManagedObjectContext: managedContext) 


     check.setValue(subject, forKey: "name") 


     do { 
      try managedContext.save() 

     } catch let error as NSError { 
      print("Could not save \(error), \(error.userInfo)") 
     } 
    } 



} 

我所有的插座和IBActions都鏈接到故事板上的按鈕和表格。

+1

請編輯您的帖子,以顯示相關的代碼 – Arc676

+2

你有鏈接到較早的ViewController一個按鈕,它現在去掉? 如果是這樣,也許一個插座仍然存在名稱addAssessment ..? – Laffen

+1

可能的重複http://stackoverflow.com/questions/3088059/what-does-this-mean-nsunknownkeyexception-reason-this-class-is-not-key。 –

回答

0

如果您正在使用故事板,那麼只需點擊AssesmentViewController並選擇它們的連接檢查器。檢查是否有任何感嘆號(丟失連接)。

正確刪除它們。然後再次運行您的項目。

enter image description here

相關問題