使用 iPhone/Objective C CGFloats:0.1 可以吗,还是应该是 0.1f?
当使用接受 CGFloat
的 iPhone Objective C 方法(例如 [UIColor colorWithRed:green:blue:]
)时,将 af 附加到常量参数以显式指定它们是否很重要作为浮点数,例如在这种情况下我应该始终键入 0.1f 而不是 0.1 吗?或者编译器是否在编译时自动将 0.1(通常是双精度型)转换为 0.1f(这是浮点数)?我不希望在运行时发生这些转换,因为它们会不必要地占用性能。
预先感谢
MrMage
When using an iPhone Objective C method that accepts CGFloat
s, e.g. [UIColor colorWithRed:green:blue:]
, is it important to append a f to constant arguments to specifiy them explicitly as floats, e.g. should I always type 0.1f rather than 0.1 in such cases? Or does the compiler automatically cast 0.1 (which is a double in general) to 0.1f (which is a float) at compile time? I don't wish to have these casts happen at run time because they would unneccessarily hog performance.
Thanks in advance
MrMage
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这并不重要;在需要单精度常量的地方使用双精度常量不会破坏任何东西。
但是,如果您已打开有关隐式 64 位到 32 位转换的警告,并且正在为 32 位架构(我相信包括 iPhone)进行构建,那么您将需要简单地使用单精度常量以避免收到该警告。
(或者,您可以将该设置显式设置为关闭,并通过架构条件将其针对 64 位架构打开。但是,目前仅当您还在 Mac 中使用某些代码时才重要应用。)
It's not important; it won't break anything to use a double-precision constant where a single-precision constant is expected.
However, if you have turned on the warning about implicit 64-bit-to-32-bit conversions and are building for 32-bit architectures (which I believe includes the iPhone), then you'll want to use single-precision constants simply to avoid getting that warning.
(Alternatively, you could set that setting to explicitly off, with an architecture condition turning it on for 64-bit architectures. But that currently only matters if you're also using some of your code in a Mac application.)