判断 NSManagedObject 属性是否可选
我正在使用 Core Data,并且有一个名为 Person
的 NSManagedObject 子类(它是 Core Data 中的一个实体)。 Person 对象具有多个属性(例如,firstName
、lastName
等)和关系(例如,friends
、coWorkers
)。其中一些属性是可选的,而其他属性是强制性的。
给定一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在基子类中编写一个函数
You can write a function in your base subclass