-attributeKeys and Core Data
-attributeKeys and Core Data
- Subject: -attributeKeys and Core Data
- From: Daniel DeCovnick <email@hidden>
- Date: Mon, 2 Apr 2007 15:46:42 -0700
The background to this question is rather long, so I've put the
question at the top in case anyone can answer without background.
Question: does Core Data set up NSClassDescriptions for Entities that
do not have custom classes, and if not, will -attributeKeys work
anyway? If not, how do I get the same effect as the sample code at
the bottom of this email?
Background:I have a CoreData application with one central entity and
a bunch of to-one relationships to other outlying entities,
essentially subparts of the central entity. Almost all of the
properties of each of the outlying entities are booleans; most of the
app is essentially a checklist (progress notes, actually, as well as
rudimentary phone call tracking facility).
I needed to get the list of boolean values into an attributed string
for putting in a view for printing, but in the form of check marks or
blanks, not 1's and 0's (1's and 0's plus find and replace is out of
the question).
At first, I was doing this via a series of strings each set to either
a string containing the unichar 0x221A (√) or @"". This meant a lot of
//checkMark = √
NSString *is90801checkOrBlank;
if ([[self valueForKeyPath:@"serviceProvided.is90801"] boolValue])
is90801checkOrBlank = checkMark;
else is90801checkOrBlank = @"";
which I would then create an attributed string with: @"Service
Provided: 90801 - initial Consultation? %@ ..." , is90801CheckOrBlank.
Eventually I figured out I could shorten the if-else:
is90801checkOrBlank = ((YES == [[self
valueForKeyPath:@"serviceProvided.is90801"] boolValue]) ? checkMark :
@"");
Still a pain, when you've got 60+ fields to go through, organized in
a dozen different entities. Plus there was the "other" issue: some
sections had "other" fields and "isOther" booleans, which just gummed
things up.
So I wondered if I could make it a bit more generic, and I wrote this:
-(NSDictionary *)checkMarks:(NSString *)checkMark andBlanks:(NSString
*)blank andOther:(NSString *)otherAttr given:(NSString *)isOtherAttr
forRelationship:(NSString *)relationship
{
NSArray *relationshipKeys = [[self valueForKey:relationship]
attributeKeys];
NSEnumerator *attr = [relationshipKeys objectEnumerator];
NSMutableArray *checksOrBlanksAndOther = [NSMutableArray array];
while (nil != [attr nextObject])
{
if (YES == [attr isNotEqualTo:otherAttr])
[checksOrBlanksAndOther addObject: ((YES == [[self valueForKeyPath:
[NSString stringWithFormat:@"%@.%@", relationship, attr]]
boolValue]) ? checkMark : blank)];
else
[checksOrBlanksAndOther addObject: ((YES == [[self valueForKeyPath:
[NSString stringWithFormat:@"%@.%@", relationship, isOtherAttr]]
boolValue]) ? [self valueForKeyPath:[NSString stringWithFormat:@"%@.%
@", relationship, otherAttr]] : blank)];
}
return [NSDictionary dictionaryWithObjects:checksOrBlanksAndOther
forKeys:relationshipKeys];
}
And then, while I'd have to call this method once for each
relationship off the central entity and objectForKey: a bunch of
times when constructing the attributed string, it would be much less
painful.
The weakness possibly lies in the first line of the method; the
documentation specifies that -attributeKeys references an
NSClassDescription, but nothing in the documentation mentions whether
Core Data will set up an NSClassDescription for entities that do not
subclass NSManagedObject. So... does it work the way I think it does,
and if not, how do I make it work properly?
Thanks,
Dan
Daniel DeCovnick
danhd123 at mac dot com
Softyards Software
http://www.softyards.com
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden