Reputation: 12431
I am hitting this error when implementing core data.
I have created a entity 'FlashCard' with the attribute 'question' and 'answer'. Both the attributes are of NSString type.
After inserting a new object into the NSManaged Object, I tried to set the 2 attributes as seen below.
NSManagedObject *newCard = [NSEntityDescription insertNewObjectForEntityForName:@"FlashCard" inManagedObjectContext:self.managedObjectContext];
newCard.question = thisQuestion;
newCard.answer = thisAnswer;
But when I try to compile the code, I am hitting the error "Request for member 'question' in something is not a structure or union'. I get the same error for newCard.answer line.
Any advise on how to resolve this is greatly appreciated!
Zhen
Upvotes: 1
Views: 712
Reputation: 5473
You've declared newCard as an NSManagedObject, then tried to access properties that NSManagedObject doesn't define.
Core Data gives you the option of using a custom subclass of NSManagedObject to represent entities. If you're doing this then, as others have suggested, you need to declare newCard as an instance of that subclass (in case you're not doing this, you'll have to write the class and declare the properties yourself if you want to use the 'dot' property syntax --- core data doesn't automatically create a subclass of NSManagedObject for each entity type)
Also, you don't have to use your own subclass or write accessors just to access a managed object's attributes and relationships. If you don't need to add any custom logic to FlashCard yet, you can use key value coding on an NSManagedObject instead. This would work fine:
NSManagedObject *newCard = [NSEntityDescription insertNewObjectForEntityForName:@"FlashCard" inManagedObjectContext:self.managedObjectContext];
[newCard setValue: thisQuestion forKey: @"question"];
[newCard setValue: thisAnswer forKey: @"answer"];
Upvotes: 2
Reputation: 6450
#import "FlashCard.h"
Is "FlashCard.h" included at the top of this file?
Upvotes: 1
Reputation: 7148
Your newCard
instance should be of type FlashCard
not NSManagedObject
; otherwise, the compiler won't know that newCard
has the properties question
and answer
.
FlashCard *newCard = (FlashCard *)[NSEntityDescription insertNewObjectForEntityForName:@"FlashCard" inManagedObjectContext:self.managedObjectContext];
newCard.question = thisQuestion;
newCard.answer = thisAnswer;
Upvotes: 5