views:

36

answers:

1

I have some automatic de-serialization code that will set an object's properties using KVC. I need to add de-serialization support for primitives (int, double, float), but I am unable (or unsure of how) to use "setValue: forKey:" with primitives. The property lookups must be performed at runtime. Any ideas? Thanks.

+1  A: 

When you obtain such a value through valueForKey:, it will box the value up for you in an NSNumber (or NSValue, for certain structure types) object.

You just need to do the same thing the other way: Box the desired value up yourself, and pass that object in. setValue:forKey: will unbox it before passing it to the property's accessor/storing it in the relevant ivar.

Peter Hosey