Search code examples
objective-ccocoamacosdictionaryprimitive

Is there a way to map objects to primitives?


This may be a hypothetical question, I'm not sure.

Coming from a C# background, I've always missed object-oriented primitives in Objective-C. Instead, you have to endlessly convert from BOOL and int to NSNumber and back again, creating unwieldy and potentially buggy code.

Is it technically possible to globally map all the primitive value types, such as int, BOOL, float, etc., to their object-oriented counter-pieces, i.e. NSNumber?


Solution

  • Technically possible? Sure. Totally.

    See CF*Dictionary and/or NSMapTable. Both allow mappings between Objective-C instances and primitive and/or non-Objective-C types.

    You'd obviously have to write some code to do the lookups, as needed, (or modify the compiler to do it for you. Overall, probably not worth doing so as it'll be complex and offer more moving parts to fail.