尝试将记录保存到核心数据时遇到问题。这个想法是将简单的学生信息保存到核心数据库中。您可以在下面看到详细信息。
我的实体课程:
import Foundation
import CoreData
class Student: NSManagedObject {
@NSManaged var id: String
@NSManaged var firstname: String
@NSManaged var lastname: String
@NSManaged var age: NSNumber
@NSManaged var email: String
}
代码是:
import UIKit
import CoreData
class AddControllerViewController: UIViewController {
let managedObjectContext = (UIApplication.sharedApplication().delegate as AppDelegate).managedObjectContext
@IBOutlet var id: UITextField!
@IBOutlet var firstname: UITextField!
@IBOutlet var lastname: UITextField!
@IBOutlet var age: UITextField!
@IBOutlet var email: UITextField!
@IBAction func save(sender: AnyObject) {
let entityDescripition = NSEntityDescription.entityForName("Student", inManagedObjectContext: managedObjectContext!)
let student = Student(entity: entityDescripition!, insertIntoManagedObjectContext: managedObjectContext)
student.id = id.text
student.firstname = firstname.text
student.lastname = lastname.text
student.age = age.text.toInt()!
student.email = email.text
managedObjectContext?.save(nil)
navigationController?.popViewControllerAnimated(true)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
以及我收到的错误消息:
2014-10-14 16:31:42.098 Registration[3829:119352] -[Registration.Student setId:]: unrecognized selector sent to instance 0x7fc667a77070
2014-10-14 16:31:42.105 Registration[3829:119352] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Registration.Student setId:]: unrecognized selector sent to instance 0x7fc667a77070'
之前是否有人有类似的错误并找到解决方法?
当我提出断点时,我得到了这些:
2014-10-14 21:52:49.227 Registration[513:9236] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Registration.Student setId:]: unrecognized selector sent to instance 0x7fe441c3d100'
*** First throw call stack:
(
0 CoreFoundation 0x00000001062593f5 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x0000000107d87bb7 objc_exception_throw + 45
2 CoreFoundation 0x000000010626050d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x00000001061b87fc ___forwarding___ + 988
4 CoreFoundation 0x00000001061b8398 _CF_forwarding_prep_0 + 120
5 Registration 0x0000000105cbc887 _TFC12Registration27AddControllerViewController4savefS0_FPSs9AnyObject_T_ + 935
6 Registration 0x0000000105cbcd02 _TToFC12Registration27AddControllerViewController4savefS0_FPSs9AnyObject_T_ + 66
7 UIKit 0x0000000106ae49ee -[UIApplication sendAction:to:from:forEvent:] + 75
8 UIKit 0x0000000106ae49ee -[UIApplication sendAction:to:from:forEvent:] + 75
9 UIKit 0x0000000106beabd0 -[UIControl _sendActionsForEvents:withEvent:] + 467
10 UIKit 0x0000000106be9f9f -[UIControl touchesEnded:withEvent:] + 522
11 UIKit 0x0000000106b2a3b8 -[UIWindow _sendTouchesForEvent:] + 735
12 UIKit 0x0000000106b2ace3 -[UIWindow sendEvent:] + 683
13 UIKit 0x0000000106af7ae1 -[UIApplication sendEvent:] + 246
14 UIKit 0x0000000106b04bad _UIApplicationHandleEventFromQueueEvent + 17370
15 UIKit 0x0000000106ae0233 _UIApplicationHandleEventQueue + 1961
16 CoreFoundation 0x000000010618ead1 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
17 CoreFoundation 0x000000010618499d __CFRunLoopDoSources0 + 269
18 CoreFoundation 0x0000000106183fd4 __CFRunLoopRun + 868
19 CoreFoundation 0x0000000106183a06 CFRunLoopRunSpecific + 470
20 GraphicsServices 0x000000010a3489f0 GSEventRunModal + 161
21 UIKit 0x0000000106ae3550 UIApplicationMain + 1282
22 Registration 0x0000000105cc070e top_level_code + 78
23 Registration 0x0000000105cc074a main + 42
24 libdyld.dylib 0x0000000108561145 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException