1

When I try to access an empty property after creating an NSManagedObject, it gives me a EXC_BAD_ACCESS (code=1, address=0x0)

let m = NSEntityDescription.insertNewObjectForEntityForName(entityName, inManagedObjectContext: context) as MyManagedEntity

println(m.numberProperty) // this is OK
println(m.stringProperty) // this gives me the exception
println(m.dateProperty) // this gives me the exception

Any Ideas how to solve it?

EDIT:

I'm also getting EXC_BREAKPOINT(code=EXC_I386_BPT, subcode-0x0) in my Tests when casting to MyManagedEntity

Rodrigo Ruiz
  • 4,248
  • 6
  • 43
  • 75

1 Answers1

2

Did you specify default values for your attributes in your XCDataModel?

If you generate an NSManagedObject subclasses you can change any attribute to an optional by adding a question mark to the declaration:

@NSManaged var stringProperty: String?
@NSManaged var dateProperty: NSDate?

Elsewhere you can do:

println(m.stringProperty?)
if m.stringproperty != nil {
// do stuff
}
Steve Schwedt
  • 380
  • 2
  • 11