开发者

Is there a way to map objects to primitives?

开发者 https://www.devze.com 2023-02-06 04:31 出处:网络
This may b开发者_开发技巧e 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 f

This may b开发者_开发技巧e 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?


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.


I don't think so, nor do I think it would do you much good. Best to just go with the flow. If you don't ignore compiler warnings this should rarely result in buggy code.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号