Aaron Hayman
Aaron Hayman

Reputation: 8502

Use of @synthesize and value class type checking

This is probably a long shot, but I've got objects with a lot of properties. The values of these objects are populated from NSDictionary's created from a database request. Because of this, there may be NSNull values contained in those NSDictionaries that will automatically get assigned to the properties. I need the properties to automatically discard values/objects that aren't of the correct type. Currently I do it like this:

- (void) setViewID:(NSString *)viewID{
    if (!viewID || [viewID isKindOfClass:[NSString class]]) _viewID = viewID;
}

But that ends up being a lot of extra code when I've got 30-50 properties. Is there a way to synthesize this behavior? It seems like it would be a common enough requirement, but I can't seem to find a way to do it aside from writing it all out.

Upvotes: 1

Views: 57

Answers (1)

rob mayoff
rob mayoff

Reputation: 385870

Why not check for NSNull when you are going through the dictionary? E.g.

for (NSString *key in dictionary) {
    id value = [dictionary objectForKey:key];
    if (value == [NSNull null]) {
        value = nil;
    }
    [self setValue:value forKey:key];
}

Upvotes: 2

Related Questions