开发者

Compile in "Release" mode causes errors for CGRect <-> NSRect conversion

Compiling my last bui开发者_运维百科ld in Release mode i get some errors like :

passing 'NSRect' (aka 'struct _NSRect') to parameter of incompatible type 'CGRect' (aka 'struct CGRect')

It is true, i'm using NSRect as CGRect and vice versa but i don't get these errors in Debug mode.

For example, this like produces an error (not in Debug mode):

CGRect rect = [[someObject window]frame];

Now i've corrected every errors using functions like NSRectToCGRect and NSRectFromCGRect but i'm curious to understand why this happens only in Release mode.


For example, this like produces an error (not in Debug mode):

CGRect rect = [someObject window]frame];

Well, that's because that's invalid code. You're missing a [.

More to the point:

passing 'NSRect' (aka 'struct _NSRect') to parameter of incompatible type 'CGRect' (aka 'struct CGRect')

It is true, i'm using NSRect as CGRect and vice versa but i don't get these errors in Debug mode.

By default, the Debug configuration builds only the active architecture, which is the architecture of the current run destination (the right half of the Scheme pop-up, assuming you're using Xcode 4). If it's set to “My Mac 64-bit”, that would be why it's always worked.

Try this: Change the run destination to “My Mac 32-bit”, then try to Run.

See?

In 64-bit Mac OS X, NSRect is defined as CGRect, which is how you're able to freely convert between them. In 32-bit Mac OS X, NSRect is defined separately, so you can't just cast from one value type to the other. They're defined the same, so you can do pointer-aliasing or go through a union and it will work, but you can't just cast, implicitly or explicitly, because they are separate, unrelated definitions.

There are two solutions:

  • Drop support for 32-bit from your app. Set your Architectures in all configurations to “Standard Intel 64-bit”.
  • In your prefix header or in the Preprocessor Macros build setting, define the NS_BUILD_32_LIKE_64 macro to 1. When this is defined to a true value, NSRect will be defined as CGRect (and likewise the other geometry types), even on 32-bit.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜