判断 NSManagedObject 属性是否可选

发布于 2024-11-02 09:06:36 字数 663 浏览 6 评论 0原文

我正在使用 Core Data,并且有一个名为 Person 的 NSManagedObject 子类(它是 Core Data 中的一个实体)。 Person 对象具有多个属性(例如,firstNamelastName 等)和关系(例如,friendscoWorkers )。其中一些属性是可选的,而其他属性是强制性的。

给定一个 Person 对象,我如何有效地确定特定属性或关系是可选的还是强制的?

我想尝试避免对 Person 对象的每个属性进行某种低效循环,因为我需要经常调用代码,所以类似于下面的伪代码的东西将是完美的:

if ( [[aPerson getProperty:@"firstName"] isOptional] ) {

    // do stuff

}

...但我是不确定是否可以简单地做到这一点。上面的伪代码也只会检查属性,所以我猜测如果要检查的属性是关系,则需要执行其他操作。

我意识到执行单个循环并将所有强制属性的名称存储在数组中可能更有效(然后只需检查该数组是否存在属性名称),这就是我目前正在做的事情,但是不幸的是,它并不真正适合我的代码结构(因此需要一种更“按需”的方法)。

任何帮助将不胜感激:)

I am using Core Data and have a subclass of NSManagedObject called Person (which is an entity in Core Data). The Person object has several properties (e.g. firstName, lastName, etc.) and relationships (e.g. friends, coWorkers). Some of these attributes are optional, whereas others are mandatory.

Given a Person object how can I efficiently determine whether a particular property or relationship is optional or mandatory?

I want to try and avoid having to do some kind of inefficient loop through each attribute of the Person object, as I will need to call the code fairly often, so something similar to the pseudocode below would be perfect:

if ( [[aPerson getProperty:@"firstName"] isOptional] ) {

    // do stuff

}

...but I am not sure whether it could be done that simply. The above pseudocode would also only check properties, so I am guessing something else would need to be done in case the attribute being checked is a relationship.

I realize that it is probably more efficient to do a single loop and store the name of all the mandatory attributes in an array (then just check that array for the presence of the attribute name) and that's what I am doing at the moment, but unfortunately it doesn't really work with the structure of my code (thus the need for a more "on-demand" approach).

Any help would be greatly appreciated :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

嘿咻 2024-11-09 09:06:36

您可以在基子类中编写一个函数

NSDictionary *d = [[self entity] attributesByName];
NSAttributeDescription *attr = (NSAttributeDescription *)[d objectForKey:@"test"];
BOOL isopt = [attr isOptional];

You can write a function in your base subclass

NSDictionary *d = [[self entity] attributesByName];
NSAttributeDescription *attr = (NSAttributeDescription *)[d objectForKey:@"test"];
BOOL isopt = [attr isOptional];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文