10

我正在将应用程序转换为 64 位工作。Apple 文档声明该CGFloat类型是float32 位和double64 位的。

如果是这样,您如何处理值的编码/解码?例如,如果NSNumber从 a创建一个CGFloat,我应该使用NSNumber的-numberWithFloat:or方法吗?-numberWithDouble:同样的问题适用于NSCoderandNSKeyedArchiver方法-encodeFloat:forKey:-encodeDouble:forKey:

我是否必须编写一个条件宏以便-numberWithFloat:在 32 位和-numberWithDouble:64 位上调用?

如果运行 64 位版本的应用程序的用户保存了一个文件,其中这些值被编码为双精度值,并且该文件在 32 位系统上打开/取消存档,会发生什么情况?

我的应用程序中使用的所有方法CGFloats都不太可能需要 a 的精度级别double,所以我什至应该担心这个吗?

4

1 回答 1

13

What happens if a user running the 64-bit version of the app saves a file where these values are encoded as doubles and the file is opened/unarchived on a 32-bit system?

Nothing special. The file contains a double, and presumably you're sending decodeDouble:forKey: to retrieve the value, so you'll get a double. You can then cast it to CGFloat—which will lose precision if you're on a 32-bit machine, but you have to do that cast sooner or later. (Even if you hold onto the double, you'll still have to cast to CGFloat when you pass the value to Quartz/AppKit/UIKit.)

Consider the alternative: You store floats in the file on a 64-bit machine, and then load the file back in on the same machine. You and Quartz can handle the full precision of the 64-bit double type, but you don't have it, because you threw it away when you wrote out the file. If the value changed at all in the cast, that change is now permanent.

So my recommendation is to always use the double type with NSKeyedArchiver, unless you know that you are never going to use anything but float (i.e., the target API only uses float, never double). This isn't true of Quartz, so for CGFloat, use double.

于 2009-11-17T10:29:05.653 回答