使用iPhone / Objective C CGFloats:是0.1还是应该是0.1f? [英] Using iPhone/Objective C CGFloats: Is 0.1 okay, or should it be 0.1f?

查看:176
本文介绍了使用iPhone / Objective C CGFloats:是0.1还是应该是0.1f?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当使用接受 CGFloat 的iPhone Objective C方法,例如 [UIColor colorWithRed:green:blue:] ,重要的是将f附加到常量参数以明确指定为浮动,例如。在这种情况下,我应该总是输入0.1f而不是0.1?或者编译器自动将0.1(这是一般的两倍)转换为0.1f(这是一个浮点数)在编译时?我不希望这些演员在运行时发生,因为他们将不必要地表演。

When using an iPhone Objective C method that accepts CGFloats, 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.

提前感谢

MrMage

推荐答案

这不是重要的;它不会破坏任何使用双精度常数的单精度常数预期。

It's not important; it won't break anything to use a double-precision constant where a single-precision constant is expected.

但是,如果您已经打开关于隐式64位的警告,位32位转换,正在构建32位体系结构(我相信包括iPhone),那么您将需要使用单精度常量来避免发生警告。

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.

(或者,您可以将该设置设置为明确关闭,架构条件使64位体系结构处于开启状态,但目前 仅当您也是在Mac应用程序中使用一些代码。)

(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.)

这篇关于使用iPhone / Objective C CGFloats:是0.1还是应该是0.1f?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆