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

Swift Managed Object Context Save failure

简单的应用程序开始于带有添加按钮的导航控制器中的 table 视图。添加按钮会带来一个带有文本字段的新视图,单击保存会将您带回到(更新的)table 视图。我的问题是当我尝试保存 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)

}

更新

员工Class:

import Foundation
import CoreData

class Employee: NSManagedObject {

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

}

联系人class:

import Foundation
import CoreData

class Contact: NSManagedObject {

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

}

您不能将 NSManagedObject 投射到您的 class。

你必须这样做:

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

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

你也可以创建一个 NSManagedObject-class。这样就可以将其转换为 class.

#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

我能够重现您的错误消息。您收到此消息是因为您没有在核心数据模型编辑器中正确设置您的实体 class 名称。

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

看到这个previous answer for more details. You can also learn more about Core Data and namespaces here