両側で KVC を使用して値にアクセスし、GameCenter を介してオブジェクトを同期しようとしています。を使用して数値を設定setValue:forKey:
するには、それらがNSNumber
オブジェクトである必要があります。int、floatなどのエンコーディングを渡すオブジェクトも
NSValue initWithBytes:objCType:
提供します。NSValue
エンコーディングを手動でチェックする代わりに、より良い解決策がありますか?
- (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];
}
これが進むべき道である場合、KVC で処理する必要がNSNumber
ある唯一のサブクラスはありますか?NSValue