Andy
Andy

Reputation: 608

How do I access a Core Data entity's attributes in code?

I've got the following bit of code in one of my methods:

...
NSNumber *selectedRecordID = [NSNumber numberWithInt:ABRecordGetRecordID(person)];
for (NSManagedObject *managedObject in fetchedResultsController.fetchedObjects) {
    if (selectedRecordID == managedObject.contactID) { // this line generates a compiler error
     // do some stuff
     }

The indicated line generates the compiler error "Request for 'contactID' in something not a structure or a union." However, 'contactID' is an attribute of the entities retrieved by the fetched results controller, and is present in the @property declarations generated by Core Data.

What am I missing here? Thanks in advance for any help you can give.

Upvotes: 3

Views: 1225

Answers (2)

Marcus S. Zarra
Marcus S. Zarra

Reputation: 46718

You can also use KVC and avoid subclassing via:

[managedObject valueForKey:@"contactID"];

Upvotes: 4

David Gelhar
David Gelhar

Reputation: 27900

But 'contactID' is not a property of the base NSManagedObject class, it's a property of your own entity class. For the property to be recognized by the compiler, you need to declare the fetched object using the appropriate type, for example:

for (MyEntity *managedObject in fetchedResultsController.fetchedObjects) {
if (selectedRecordID == managedObject.contactID) { 
 }

Upvotes: 2

Related Questions