我目前是编程的新手。我试图保存核心数据对象,但目前我在保存Coredata时出错并显示错误。专注于保存功能。
) libc ++ abi.dylib:以NSException类型的未捕获异常终止。
//
// ViewController.swift
// HitList
//
//
import UIKit
import CoreData
class ViewController: UIViewController, UITableViewDataSource {
@IBOutlet weak var tableView: UITableView!
//Change “names” to “people” and [String] to [NSManagedObject]
var people = [NSManagedObject]()
@IBAction func addName(sender: AnyObject) {
let alert = UIAlertController(title: "New Name", message: "Add a new name", preferredStyle: .Alert)
let saveAction = UIAlertAction(title: "Save",
style: .Default,
handler: { (action:UIAlertAction) -> Void in
let textField = alert.textFields!.first
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)
}
override func viewDidLoad() {
super.viewDidLoad()
title = "\"The List\""
tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "Cell")
}
// MARK: UITableViewDataSource
func tableView(tableView: UITableView,
numberOfRowsInSection section: Int) -> Int {
return people.count
}
func tableView(tableView: UITableView,
cellForRowAtIndexPath
indexPath: NSIndexPath) -> UITableViewCell {
let cell =
tableView.dequeueReusableCellWithIdentifier("Cell")
let person = people[indexPath.row]
cell!.textLabel!.text =
person.valueForKey("name") as? String
return cell!
}
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
do {
try managedContext.save()
//5
people.append(person)
} catch let error as NSError {
print("Could not save \(error), \(error.userInfo)")
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
错误:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIButton copyWithZone:]: unrecognized selector sent to instance 0x7f8089631d50'
*** First throw call stack:
(
0 CoreFoundation 0x000000010d6dde65 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x000000010f41ddeb objc_exception_throw + 48
2 CoreFoundation 0x000000010d6e648d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x000000010d63390a ___forwarding___ + 970
4 CoreFoundation 0x000000010d6334b8 _CF_forwarding_prep_0 + 120
5 CoreFoundation 0x000000010d5ad652 CFStringCreateCopy + 242
6 libswiftCore.dylib 0x000000010f9d52d8 _TTSf4g_d___TFSSCfMSSFT12_cocoaStringPSs9AnyObject__SS + 104
7 libswiftCore.dylib 0x000000010f99c9c3 _TFSSCfMSSFT12_cocoaStringPSs9AnyObject__SS + 19
8 libswiftFoundation.dylib 0x000000010fd6e260 _TF10Foundation24_convertNSStringToStringFGSqCSo8NSString_SS + 16
9 Gill 0x000000010d168486 _TToFC4Gill14ViewController8saveNamefS0_FSST_ + 54
10 UIKit 0x000000010defc194 -[UIApplication sendAction:to:from:forEvent:] + 92
11 UIKit 0x000000010e06b6fc -[UIControl sendAction:to:forEvent:] + 67
12 UIKit 0x000000010e06b9c8 -[UIControl _sendActionsForEvents:withEvent:] + 311
13 UIKit 0x000000010e06aaf8 -[UIControl touchesEnded:withEvent:] + 601
14 UIKit 0x000000010df6b49b -[UIWindow _sendTouchesForEvent:] + 835
15 UIKit 0x000000010df6c1d0 -[UIWindow sendEvent:] + 865
16 UIKit 0x000000010df1ab66 -[UIApplication sendEvent:] + 263
17 UIKit 0x000000010def4d97 _UIApplicationHandleEventQueue + 6844
18 CoreFoundation 0x000000010d609a31 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
19 CoreFoundation 0x000000010d5ff95c __CFRunLoopDoSources0 + 556
20 CoreFoundation 0x000000010d5fee13 __CFRunLoopRun + 867
21 CoreFoundation 0x000000010d5fe828 CFRunLoopRunSpecific + 488
22 GraphicsServices 0x0000000111d1dad2 GSEventRunModal + 161
23 UIKit 0x000000010defa610 UIApplicationMain + 171
24 Gill 0x000000010d16b2ad main + 109
25 libdyld.dylib 0x000000010ff2f92d start + 1
26 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)