目标 C:请求非结构或联合中的成员 XXX。 (核心数据)

发布于 2024-11-02 02:44:34 字数 474 浏览 4 评论 0原文

我在实现核心数据时遇到此错误。

我创建了一个实体“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

青芜 2024-11-09 02:44:35

您已将 newCard 声明为 NSManagedObject,然后尝试访问 NSManagedObject 未定义的属性。

Core Data 使您可以选择使用 NSManagedObject 的自定义子类来表示实体。如果您这样做,那么正如其他人所建议的那样,您需要将 newCard 声明为该子类的实例(如果您不这样做,则必须自己编写该类并声明属性(如果您想要)使用“点”属性语法 --- 核心数据不会自动为每个实体类型创建 NSManagedObject 的子类)

此外,您不必使用自己的子类或编写访问器来访问托管对象的属性和关系。如果您还不需要向 FlashCard 添加任何自定义逻辑,则可以在 NSManagedObject 上使用键值编码。这会工作得很好:

NSManagedObject *newCard = [NSEntityDescription insertNewObjectForEntityForName:@"FlashCard" inManagedObjectContext:self.managedObjectContext];

[newCard setValue: thisQuestion forKey: @"question"];
[newCard setValue: thisAnswer forKey: @"answer"];

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"];
云淡风轻 2024-11-09 02:44:35
#import "FlashCard.h"

该文件的顶部是否包含“FlashCard.h”?

#import "FlashCard.h"

Is "FlashCard.h" included at the top of this file?

铁轨上的流浪者 2024-11-09 02:44:34

您的 newCard 实例的类型应该是 FlashCard 而不是 NSManagedObject;否则,编译器不会知道 newCard 具有属性 questionanswer

FlashCard *newCard = (FlashCard *)[NSEntityDescription insertNewObjectForEntityForName:@"FlashCard" inManagedObjectContext:self.managedObjectContext];
newCard.question = thisQuestion;
newCard.answer = thisAnswer;

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文