Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Core data 由于未捕获异常而终止应用程序';尝试保存核心数据时发生NSInvalidArgumentException_Core Data_Swift - Fatal编程技术网

Core data 由于未捕获异常而终止应用程序';尝试保存核心数据时发生NSInvalidArgumentException

Core data 由于未捕获异常而终止应用程序';尝试保存核心数据时发生NSInvalidArgumentException,core-data,swift,Core Data,Swift,我在尝试将记录保存到核心数据时遇到问题。其想法是将简单的学生信息保存到核心数据数据库中。你可以看到下面的细节 我的实体类: import Foundation import CoreData class Student: NSManagedObject { @NSManaged var id: String @NSManaged var firstname: String @NSManaged var lastname: String @NSManaged var age: NSNumber

我在尝试将记录保存到核心数据时遇到问题。其想法是将简单的学生信息保存到核心数据数据库中。你可以看到下面的细节

我的实体类:

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

放置断点以捕获所有异常,并查看其崩溃的确切位置。是否在这一行
student.id=id.text
?我用常量值替换了所有文本输入,同样的错误仍然发生。student.id=“2000”等。是否设置了断点?如果将作业注释为
id
,它是否会在
firstname
行崩溃?(我想知道它是否不喜欢将“id”作为一个名称,或者不善于处理
学生
),但正如您在我的学生NSManagedObject上看到的,有一个id声明为字符串。如果您看到错误消息,我只是想知道“setId”是从哪里来的。“-[Registration.Student setId:]:无法识别的选择器…”