开发者

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 er开发者_运维技巧ror "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.


You can also use KVC and avoid subclassing via:

[managedObject valueForKey:@"contactID"];


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) { 
 }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜