views:

289

answers:

2

I have an abstract entity called Block which contains two attributes: column and order (which are not optional), and one relationship, thing, where it is the detail of a 1->M. I have another entity, Whatever, that has Block as its parent, and adds one attribute, someNumber.

My code looks like this:

Whatever *block = (Whatever *)[NSEntityDescription insertNewObjectForEntityForName:@"Whatever" inManagedObjectContext:managedObjectContext];
block.order = 0;
block.column = 0;
block.thing = self.thing;

When I try to save, I get this error:

Failed to save to data store: Operation could not be completed. (Cocoa error 1560.)
DetailedError: {
  NSLocalizedDescription = "Operation could not be completed. (Cocoa error 1570.)";
  NSValidationErrorKey = column;
  NSValidationErrorObject = <Whatever: 0x5124890> (entity: someWhatever; id: 0x511b4e0 <x-coredata:///Whatever/t718B63A4-927B-4D88-A9E6-7F61CF9621675> ; 
  data: {
    column = nil;
    thing = 0x54367a0 <x-coredata://E6648244-E5FC-4202-B5F9-C7A91BACF8DA/Thing/p2>;
    order = nil;
    someNumber = 0;
  });

I don't understand why it says that column and order are nil, as I've just set them the line before, so this shouldn't be a problem.

I've tried using the [block setColumn:0] style as well, without success.

Any help would be appreciated. Thanks!

+4  A: 

You are setting them to nil since nil is just a null or zero pointer value.

Core Data properties must be set to objects (as opposed to primitive types).

Integers and floating point numbers are NSNumber objects.

I like to use the numberWith* convenience constructors.

For example:

block.order = [NSNumber numberWithInteger:0];
block.column = [NSNumber numberWithInteger:0];
gerry3
Thanks. I'm still getting my objective-c-legs, as it were. :-)
Tim Sullivan
A: 

To expand on gerry3's answer, a great way to ease coding with Core Data is to use Rentzsch's mogenerator. It would allow you to do:

block.orderValue = 0;
block.columnValue = 0;
Johan Kool