I'm trying to sync objects over GameCenter, accessing their values with KVC on both sides. Setting numeric values using setValue:forKey:
requires them to be NSNumber
objects.
NSValue initWithBytes:objCType:
gives me NSValue
objects even passing encodings for int, float and such.
Do you guys have a better solution instead of checking the encoding manually?
- (NSValue*)smartValueWithBytes:(void*)value objCType:(const char*)type
{
if (0 == strcmp(type, @encode(int)))
{
int tmp;
memcpy(&tmp, value, sizeof(tmp));
return [NSNumber numberWithInt:tmp];
}
if (0 == strcmp(type, @encode(BOOL)))
{
BOOL tmp;
memcpy(&tmp, value, sizeof(tmp));
return [NSNumber numberWithBool:tmp];
}
//etc...
return [NSValue valueWithBytes:value objCType:type];
}
If this is the way to go, is NSNumber
the only NSValue
subclass i need to take care of for KVC?