iOS how can I dump multiple properties of an object into a dictionary using a predicate or KVC?

Viewed 575

I have a class that got bloated with properties and now there's about 30 of them, most of which are integer enumerated types.

My code currently uses this in a bunch of places, and I'm trying to gently move to the new dictionary representation.

I'm looking to create a dictionary out of this object, but only include values that are non-0, (values that have some data).

Is there some objective-c key value coding magic that can help me simplify writing this method?

@property(nonatomic)kGrade grade;
@property(nonatomic)kQuality quality;
//a whole bunch more properties

    -(NSMutableDictionary*)itemAsDictionary
    {
        if(itemDictionary !=nil)
        {
            return itemDictionary;
        }else
        {
            itemDictionary = [[NSMutableDictionary alloc] initWithCapacity:40];


            //I really dont want to write a whole bunch of such statements
            if(self.grade>0)
            {
                [itemDictionary setObject:@(self.grade) forKey:@"grade"];
            }
            //add 39 other items

        }
        return itemDictionary;
    }
2 Answers
Related