Hi Matti,

On 2/06/11, Motti Shneor <[email protected]> wrote:

@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;
}

I recall having some problems looping over attributes too. Here is some working code which solves the (known) problem that Xcode 4 no longer leaves uninitialized integer attributes as nil:

    NSEntityDescription * anEntity = self.entity;
// 22 Mar 11 - brute force work around for Xcode 4's setting of numbers to zero.
    for (NSPropertyDescription * property in anEntity)
    {
        if ([property isKindOfClass: [NSAttributeDescription class]])
        {
NSAttributeDescription * attribute = (NSAttributeDescription *) property;
            NSAttributeType anAttributeType = [attribute attributeType];
if ( NSInteger16AttributeType == anAttributeType || NSFloatAttributeType == anAttributeType || NSInteger32AttributeType == anAttributeType || NSInteger64AttributeType == anAttributeType || NSDecimalAttributeType == anAttributeType ||
                    NSDoubleAttributeType       == anAttributeType
                )
            {
                [self setValue: nil forKey: attribute.name];
            }
        }
    }

Notice how I had to loop on NSPropertyDescription rather than NSAttributeDescription. Perhaps that will help you?

Cheers,

Steve


_______________________________________________

Cocoa-dev mailing list ([email protected])

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [email protected]

Reply via email to