开发者

Determine whether NSManagedObject attribute is optional

开发者 https://www.devze.com 2023-02-28 08:06 出处:网络
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

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 attribut开发者_StackOverflow社区es 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 :)


You can write a function in your base subclass

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

精彩评论

暂无评论...
验证码 换一张
取 消