有没有办法将对象映射到基元?
这可能是一个假设性问题,我不确定。
由于具有 C# 背景,我一直怀念 Objective-C 中面向对象的原语。相反,您必须无休止地从 BOOL
和 int
到 NSNumber
进行转换,然后再转换回来,从而创建笨拙且可能存在错误的代码。
在技术上是否可以将所有原始值类型(例如 int
、BOOL
、float
等)全局映射到其面向对象对应部分,即 NSNumber
?
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
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
技术上可行吗?当然。完全。
请参阅
CF*Dictionary
和/或NSMapTable
。两者都允许 Objective-C 实例与原始和/或非 Objective-C 类型之间的映射。显然,您必须根据需要编写一些代码来执行查找(或者修改编译器来为您执行此操作)。总的来说,可能不值得这样做,因为它会很复杂并且会提供更多的移动部件来失败。
Technically possible? Sure. Totally.
See
CF*Dictionary
and/orNSMapTable
. 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.