Rodrigo Ruiz
Rodrigo Ruiz

Reputation: 4355

CoreData EXC_BAD_ACCESS (code=1, address=0x0) when access empty property

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

Upvotes: 1

Views: 1910

Answers (1)

Steve Schwedt
Steve Schwedt

Reputation: 400

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
}

Upvotes: 2

Related Questions