How to check xcode version in code

I have a piece of code which can be build successfully in xcode 10.x version, but today the code build failed after I grade the xcode to 11.1 version, so I want to see if there's xcode version macro can be used in build phase, like this:


#if XCODE_SDK_MAJ_VERSION <11

code line 1

code line 2

#else

code line 2

#endif


Any comments?

Replies

I have never seen a way to test XCode version.


But you can test Swift version, which should give you interesting info.


#if swift(>=5.0)
print("Running Swift 5.0 or later")
#else
print("Running old Swift")
#endif

we're using the OC not swift in the project, is there OC related? But, IMO, the best solution is to check the xcode version during build time... since the same code has different build result for different xcode version: xcode earlier than 11.1 is OK, but will fail for 11.1 version which I just upgrade this morning :-(

Xcode itself causing the error? Never experienced that, but I have experienced JSON related bugs for iOS 12 that were fixed with iOS 13, so I had to use different code for older versions of iOS:

Code Block
if #available(iOS 13.0, *) {
<# iOS 13 and Newer Code #>
} else {
<# Older Version Code #>
}

I had to use it again for a new project to adopt the UIScene lifecycle in a iOS 12.4 project.
You can check the SDK version like so:
Code Block
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 140000 // Xcode 12 and iOS 14, or greater
// Xcode 11 will not see this code.
#endif
If you also want to build Objective C for macOS:

Code Block
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 140000 __MAC_OS_X_VERSION_MAX_ALLOWED >= 101500
...
#endif