I'm using the Swift boilerplate code for Core Data in a fresh project. My .xcdatamodeld
file has a single entity defined (Task
) with a single attribute (name
).
I have a Task.swift
file that looks like this:
import CoreData
class Task: NSManagedObject {
@NSManaged var name: String
}
When I run this, it works:
var firstTask = NSEntityDescription.insertNewObjectForEntityForName("Task",
inManagedObjectContext: managedObjectContext) as NSManagedObject
firstTask.setPrimitiveValue("File my TPS reports", forKey: "name")
var error: NSError?
managedObjectContext.save(&error)
I can even go into the SQLite database being used by the iOS simulator and confirm that the row was added.
However, when I run the exact same code as above but with as Task
instead of as NSManagedObject
, I get a crash with the error message Thread 1: EXC_BREAKPOINT (code=EXC_I386_BPT, subcode=0x0)
, associated with the var firstTask
… line. If I continue execution, I get EXC_BAD_ACCESS
and 0 misaligned_stack_error_
at the top of Thread 1 each time I advance it.
Why might this cast lead to all this?
question from:
https://stackoverflow.com/questions/24154117/how-come-i-can-cast-to-nsmanagedobject-but-not-to-my-entitys-type 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…