目标 C:请求非结构或联合中的成员 XXX。 (核心数据)
我在实现核心数据时遇到此错误。
我创建了一个实体“FlashCard”,其属性为“问题”和“答案”。这两个属性都是 NSString 类型。
将新对象插入 NSManaged 对象后,我尝试设置 2 个属性,如下所示。
NSManagedObject *newCard = [NSEntityDescription insertNewObjectForEntityForName:@"FlashCard" inManagedObjectContext:self.managedObjectContext];
newCard.question = thisQuestion;
newCard.answer = thisAnswer;
但是,当我尝试编译代码时,出现错误“请求成员‘问题’不是结构或联合”。newCard.answer 行也出现同样的错误。
有关如何解决此问题的任何建议是非常赞赏
!
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您已将 newCard 声明为 NSManagedObject,然后尝试访问 NSManagedObject 未定义的属性。
Core Data 使您可以选择使用 NSManagedObject 的自定义子类来表示实体。如果您这样做,那么正如其他人所建议的那样,您需要将 newCard 声明为该子类的实例(如果您不这样做,则必须自己编写该类并声明属性(如果您想要)使用“点”属性语法 --- 核心数据不会自动为每个实体类型创建 NSManagedObject 的子类)
此外,您不必使用自己的子类或编写访问器来访问托管对象的属性和关系。如果您还不需要向 FlashCard 添加任何自定义逻辑,则可以在 NSManagedObject 上使用键值编码。这会工作得很好:
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:
该文件的顶部是否包含“FlashCard.h”?
Is "FlashCard.h" included at the top of this file?
您的
newCard
实例的类型应该是FlashCard
而不是NSManagedObject
;否则,编译器不会知道newCard
具有属性question
和answer
。Your
newCard
instance should be of typeFlashCard
notNSManagedObject
; otherwise, the compiler won't know thatnewCard
has the propertiesquestion
andanswer
.