Re: CoreData problem - AttributeDescription seems invalid --- What am I doing wrong?
Re: CoreData problem - AttributeDescription seems invalid --- What am I doing wrong?
- Subject: Re: CoreData problem - AttributeDescription seems invalid --- What am I doing wrong?
- From: Motti Shneor <email@hidden>
- Date: Tue, 07 Jun 2011 21:36:51 +0300
Thanks everyone. I knew it had to be something stupid....
I still feel that iterating over a dictionary means iterating over its CONTENTS, not its KEYS --- but that's just my weird brain. Finally I found it in the documentation as well.
Following Evadne Wu's advice it now works flawlessly.
On 02/06/2011, at 18:23, Evadne Wu wrote:
> Hello Motti,
>
> I believe -attributesByName returns a dictionary, where the keys are names (NSStrings) and the objects are NSAttributeDescription objects, so when you say `for (id something in aDictionary)` that `something` is going to be the key, not the object value [1], and you will just get the attribute description by using that key.
>
> This *might* work:
>
> - (NSMutableDictionary *)attributesAsDictionary {
> NSDictionary *attributesByName = self.entity.attributesByName
> NSMutableDictionary *returned = [NSMutableDictionary dictionaryWithCapacity:[attributesByName count]];
> for (NSString *aKey in [attributesByName allKeys]) {
> [returned setValue:[self valueForKey:aKey] forKey:aKey]; // nil allowed if my memory stands
> }
> return returned;
> }
>
> [1]: I believe you’ve assumed that for…in enumerates object values for NSDictionary objects?
>
> -ev
>
> On Jun 2, 2011, at 20:01, Motti Shneor wrote:
>
>> Hello everyone.
>>
>> We work with a CoreData model that is memory-based. However, at times I would want to (partially) save an entity to the UserDefaults, as a dictionary. For that, I wrote a little NSManagedObject extension (Category) with the following method:
>>
>> @interface NSManagedObject (OURExtension)
>> @property (readonly) NSMutableDictionary *attributesAsDictionary;
>> @end
>> - (NSMutableDictionary *)attributesAsDictionary {
>> NSMutableDictionary *attributesDictionary = [NSMutableDictionary dictionaryWithCapacity:[self.entity.attributesByName count]];
>> for (NSAttributeDescription* attribute in self.entity.attributesByName ) {
>> NSString *attribName = [attribute name];
>> id attribValue = [self valueForKey:attribName];
>> if (attribName && attribValue)
>> [attributesDictionary setObject:attribValue forKey:attribName];
>> }
>> return attributesDictionary;
>> }
>>
>> Running the method for a nice entity that I just Saved (validated) and Fetched again (to be sure it's in the store) --- the method crashes horribly on the NSString *attribName = [attribute name];
>>
>> If I step inside with the debugger, I see things I can't understand. I get into the loop, and for the first attribute ---
>> 1. Debugger claims 'attribute's type is CFStringRef * instead of NSAttributeDescription*
>> 2. Summary for attribute is "telephoneID" which is reasonable --- one of my attributes is named like that.
>> 3. drilling down, the private "_name" iVar for the attribute is nil but when I try to get it :
>> 4. NSString *attribName = [attribute name] crashes and debugger looses all track of stack or anything.
>>
>> I looked for "valid" "isValid" or similar properties in NSAttributeDescription and its ancestor NSPropertyDescription to no avail. Nothing in the docs will tell me WHEN is it right to use the NSAttributeDescription in the above manner.
>>
>> Ideas anyone?
Motti Shneor,
Senior Software Engineer and Team Leader, Spectrum Reflections LTD.
---
ceterum censeo microsoftiem delendam esse
---
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please 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