skip to Main Content

Xcode14 is throwing compile error –

enter image description here

When I use clipsToBounds property in Mac OS 14 with Xcode 15 it works.
I need to run code on both Xcode 15 and Xcode 14.

I added availability check, but its failing at compile time.

any suggestion?

2

Answers


  1. The reason is that clipsToBounds ships with macOS 14 SDK, which includes Xcode 15. In Xcode below 15, this property is not available, so the compiler cannot find it and throws an error. It’s just a kind of syntax detection.

    The release note is here.

    In macOS 14, AppKit is exposing the clipsToBounds property of NSView.

    Login or Signup to reply.
  2. When you need to support both Xcode 14 and Xcode 15 while also wanting to take advantage of new APIs added in macOS 14.0, you need to make use of the __MAC_OS_X_VERSION_MAX_ALLOWED macro.

    In your specific case you would want:

    #if __MAC_OS_X_VERSION_MAX_ALLOWED >= __MAC_14_0 // Only true in Xcode 15.0+
        myNSView.clipsToBounds = YES;
    #endif
    

    clipsToBounds is a weird case since it became public in Xcode 15 but it can now be used with a deployment target back to macOS 10.9.

    The more general approach, when needing to support an API added in macOS 14.0 and not available at all prior to that, and your app has a deployment target older than macOS 14.0, all while supporting Xcode 14 and 15, then you need something like the following:

    #if __MAC_OS_X_VERSION_MAX_ALLOWED >= __MAC_14_0
        // Built with Xcode 15.0+
        if (@available(macOS 14.0, *)) {
            someObject.someNewProperty = someValue;
        } else {
            // optional code for pre-macOS 14.0
            someObject.someOldProperty = someValue;
        }
    #else
        // Built with Xcode before 15.0
        // optional code for pre-macOS 14.0
        someObject.someOldProperty = someValue;
    #endif
    

    Once you stop trying to support Xcode 14 you can remove everything between #else and #endif, and remove the #if, #else, and #endif` lines.

    The downside to this is that if you do have alternate code for older versions of macOS while supporting multiple versions of Xcode, that code gets repeated in two places.

    The following resolves the duplicate code issue:

        if (@available(macOS 14.0, *)) {
    #if __MAC_OS_X_VERSION_MAX_ALLOWED >= __MAC_14_0
            // Built with Xcode 15.0+
            someObject.someNewProperty = someValue;
    #endif
        } else {
            // optional code for pre-macOS 14.0
            someObject.someOldProperty = someValue;
        }
    

    Once you stop trying to support Xcode 14 you can remove the #if and #endif lines.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search