【发布时间】:2013-10-21 22:51:31
【问题描述】:
问题
考虑这样的布局代码:
CGFloat descriptionHeight = // height of a paragraph of text with zero or more words;
CGFloat imageHeight = // height of an image;
CGFloat yCoordinateOfSomeOtherView = fmax(descriptionHeight, imageHeight) + 5.0f;
如何重写第三行以支持 64 位 iOS 设备?
(当前代码没有考虑yCoordinateOfSomeOtherView是float还是double。)
选项
几个选项(我不确定哪个最好):
1。定义我自己的宏
#if defined(__LP64__) && __LP64__
#define cgfmax(x,y) fmaxf(x,y)
#else
#define cgfmax(x,y) fmax(x,y)
#endif
然后我可以将fmax 替换为cgfmax。
2。使用tgmath.h
This Stack Overflow answer 从 2011 年开始建议将 math.h 替换为 tgmath.h。这会将fmax 的实现替换为对每个参数调用__typeof__ 并根据参数类型返回fmax 或fmaxf。
3。忽略这个问题
由于 CGFloats 与布局相关,将float 存储到double 中可能导致的数据丢失通常是微不足道的。也就是说,它们将代表像素的一小部分。
总结
我正在寻找以前做过 32 位到 64 位转换的人的任何其他选项或有用的建议。
【问题讨论】:
-
是的,布局内容并没有真正达到 32 位的限制。所以它可能不会成为问题,但做心理锻炼以供将来参考是很有趣的。
标签: ios objective-c cocoa-touch cgfloat