I have some parsing code I'm using for serialising and deserialising objects from our web service and I've hit a bit of a problem when serialising booleans.
The serialisation looks like this:
- (NSDictionary *)dictionaryRepresentationWithMapping:(NSDictionary *)mappingDictionary
{
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc]init];
for (id key in[mappingDictionary allKeys])
{
id value = [self valueForKey:key];
if ((value != [NSNull null]) && (![value isKindOfClass:[NSNull class]]) && (value != nil))
{
[dictionary setObject:value forKey:mappingDictionary[key]];
}
}
return [NSDictionary dictionaryWithDictionary:dictionary];
}
The problem is that when I call valueForKey: on my NSManagedObject and then add this to my dictionary I end up with the value being set as if I was calling:
[dictionary setObject:@1 forKey:mappingDictionary[key]];
instead of:
[dictionary setObject:@YES forKey:mappingDictionary[key]];
This means that when I turn this into JSON, in the next stage, I'm sending 1 instead of true to the server.
So what I need is a way of retaining the fact that this is an NSNumber representing a bool as opposed to a number. I've tried asking for the class but I just get back NSNumber. Is there a way I can retain this automatically or failing that, is there a way I can consult the model to see what the attribute type was set to?