objective-c 将 int 转换为 CGFloat

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1055614/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 21:47:52  来源:igfitidea点击:

Convert int to CGFloat

iphoneobjective-cinteger

提问by Shoaibi

Any idea on how to convert int to CGFloat in objective C?

关于如何在目标 C 中将 int 转换为 CGFloat 的任何想法?

回答by Ben Gotow

In the past casting a CGFloat value using the () syntax has worked fine for me. CGFloat is just defined as "typedef float CGFloat;" so you go about casting it the same way you would a float:

过去,使用 () 语法转换 CGFloat 值对我来说效果很好。CGFloat 只是定义为“typedef float CGFloat;” 因此,您可以像使用浮点数一样投射它:

CGFloat f = (CGFloat)intVal;

or, if your value is a constant:

或者,如果您的值是一个常量:

CGFloat f = 1.10;

回答by Amr Angry

i was searching in how to do the same in swift and i find the question in first result (high ranked) so i will post the answer if had found in case someone was lucky like me :)

我正在寻找如何在 swift 中做同样的事情,我在第一个结果(排名高)中找到了这个问题,所以我会发布答案,以防万一有人像我一样幸运:)

let myCGFloat = CGFloat(myFloat)