Swift托管对象上下文保存失败

时间:2015-01-21 16:51:53

标签: ios swift core-data nsmanagedobjectcontext

简单应用程序以带有添加按钮的导航控制器中的表格视图开始。 “添加”按钮带来带有文本字段的新视图,单击“保存”将返回到(更新的)表视图。我的问题是当我尝试保存MOC时,没有任何反应,应用程序在调试器中崩溃:

First name is: John
Last name is: Doe
Phone number is: 123456789
Before Save
(lldb) 

和(****是它崩溃的线)

libswiftCore.dylib`swift_dynamicCastClassUnconditional:
0x10c385860:  pushq  %rbp
0x10c385861:  movq   %rsp, %rbp
0x10c385864:  testq  %rdi, %rdi
0x10c385867:  je     0x10c38589e               ; swift_dynamicCastClassUnconditional + 62
0x10c385869:  movabsq $-0x7fffffffffffffff, %rax
0x10c385873:  testq  %rax, %rdi
0x10c385876:  jne    0x10c38589e               ; swift_dynamicCastClassUnconditional + 62
0x10c385878:  leaq   0xb52e9(%rip), %rax
0x10c38587f:  movq   (%rax), %rax
0x10c385882:  andq   (%rdi), %rax
0x10c385885:  nopw   %cs:(%rax,%rax)
0x10c385890:  cmpq   %rsi, %rax
0x10c385893:  je     0x10c3858ad               ; swift_dynamicCastClassUnconditional + 77
0x10c385895:  movq   0x8(%rax), %rax
0x10c385899:  testq  %rax, %rax
0x10c38589c:  jne    0x10c385890               ; swift_dynamicCastClassUnconditional + 48
0x10c38589e:  leaq   0x36b7d(%rip), %rax       ; "Swift dynamic cast failed"
0x10c3858a5:  movq   %rax, 0xb4c0c(%rip)       ; gCRAnnotations + 8
0x10c3858ac:  int3   
****0x10c3858ad:  movq   %rdi, %rax
0x10c3858b0:  popq   %rbp
0x10c3858b1:  retq   
0x10c3858b2:  nopw   %cs:(%rax,%rax)

这是我的保存功能:

    @IBAction func save() {

    if let moc = (UIApplication.sharedApplication().delegate as AppDelegate).managedObjectContext {

        employee = NSEntityDescription.insertNewObjectForEntityForName("Employee", inManagedObjectContext: moc) as Employee
        contact = NSEntityDescription.insertNewObjectForEntityForName("Contact", inManagedObjectContext: moc) as Contact
        employee.contact = contact

        //println("First name is: " + employee.contact.firstName)
        employee.contact.firstName = firstNameTextField.text
        println("First name is: " + employee.contact.firstName)

        employee.contact.lastName = lastNameTextField.text
        println("Last name is: " + employee.contact.lastName)

        employee.contact.phoneNumber = phoneNumberTextField.text
        println("Phone number is: " + employee.contact.phoneNumber)

        var e: NSError?
        println("Before Save")
        if moc.save(&e) != true {
            println("After Save")
            println("insert error: \(e!.localizedDescription)")
            return
        }
        println("After Save")

    // Execute the unwind segue and go back to the home screen
    performSegueWithIdentifier("unwindToHomeScreen", sender: self)

}

更新

员工类:

import Foundation
import CoreData

class Employee: NSManagedObject {

    @NSManaged var wage: NSNumber
    @NSManaged var socialInsuranceNumber: NSNumber
    @NSManaged var contact: Contact

}

联系班级:

import Foundation
import CoreData

class Contact: NSManagedObject {

    @NSManaged var firstName: String
    @NSManaged var lastName: String
    @NSManaged var phoneNumber: String
    @NSManaged var employee: NSManagedObject

}

2 个答案:

答案 0 :(得分:3)

我能够重现您的错误消息。您收到此消息是因为未在Core Data Model Editor中正确设置实体类名称。

将核心数据模型编辑器中的实体类名称字段定义为<MyAppName>.Employee<MyAppName>.Contact

有关详细信息,请参阅此previous answer。您还可以了解有关核心数据和命名空间here的更多信息。

答案 1 :(得分:0)

您无法向您的班级投放NSManagedObject

你必须这样做:

 employee = NSEntityDescription.insertNewObjectForEntityForName("Employee", inManagedObjectContext: moc) as NSManagedObject

然后,您可以使用valueForKey - 方法访问属性。

您还可以创建NSManagedObject类。这样就可以将它投射到这个类中。

#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>

@interface Employee: NSManagedObject

@property (nonatomic, retain) NSString* firstName;
@property (nonatomic, retain) NSString* lastName;
@property (nonatomic, retain) NSString* phoneNumber;

@end

访问:

 employee = NSEntityDescription.insertNewObjectForEntityForName("Employee", inManagedObjectContext: moc) as Employee