How to determine if compiling for 64-bit iOS in Xcode

前端 未结 2 546
抹茶落季
抹茶落季 2021-02-13 00:28

Consider the following function

CGSize CGSizeIntegral(CGSize size)
{
    return CGSizeMake(ceilf(size.width), ceilf(size.height));
}

CGSi

相关标签:
2条回答
  • 2021-02-13 01:01

    To determine if you are compiling for 64-bit, use __LP64__:

    #if __LP64__
        return CGSizeMake(ceil(size.width), ceil(size.height));
    #else
        return CGSizeMake(ceilf(size.width), ceilf(size.height));
    #endif
    

    __LP64__ stands for "longs and pointers are 64-bit" and is architecture-neutral.

    According to your transition guide it applies for iOS as well:

    The compiler defines the __LP64__ macro when compiling for the 64-bit runtime.

    However, the preferred way to handle your use case is to use CGFLOAT_IS_DOUBLE. There is no guarantee that __LP64__ will always mean the CGFloat is a double, but it would be guaranteed with CGFLOAT_IS_DOUBLE.

    #if CGFLOAT_IS_DOUBLE
        return CGSizeMake(ceil(size.width), ceil(size.height));
    #else
        return CGSizeMake(ceilf(size.width), ceilf(size.height));
    #endif
    
    0 讨论(0)
  • 2021-02-13 01:19

    For Swift, and neglecting the OP's specific question involving CGFloats, etc., the following may be more swiftish:

      #if (arch(i386) || arch(arm))
         ....  // For 32-bit systems
      #else
         ....  // For 64-bit systems
      #endif
    
    
      #if (arch(x86_64) || arch(arm64))
         ....  // For 64-bit systems
      #endif
    

    On the other hand, the compile-time constants discussed in the above answer are also available in Swift, if they are preferred.

    This is mostly copied from here: https://stackoverflow.com/a/24869607/253938

    And the Apple docs about these Swift compile-time constants is here: https://developer.apple.com/library/content/documentation/Swift/Conceptual/BuildingCocoaApps/InteractingWithCAPIs.html (at the very bottom of the page).

    0 讨论(0)
提交回复
热议问题