如果在 Xcode 的特定版本上,则只有 运行 代码

Only run code if on a certain version of Xcode

我正在制作一个使用 variants variable of AVAsset 的库。那不那么重要;重要的是它有一个 Xcode 13+ 的要求。这个库可能被应用程序使用 运行ning Xcode 12. 是否有一个编译器标志可以 运行 只有在特定版本的 Xcode 上才能编码?

类似于 Swift 版本的做法:

#if swift(>=5.0)
   // code
#else
   // code
#endif

我愿意

#if xcode(>=13.0)

   // use variants

#endif

据我所知,无法直接查看 Xcode 版本。但是,Xcode13支持iOS15,Xcode 12 doesn’t。您可以使用它作为变通方法来检查 Xcode 版本是否为 13 或更高版本。

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 150000 // iOS 15 or greater
// Xcode 12 will not see this code.
#else
// Xcode 12 and lower will see this
#endif

我发现 #if compiler(>=5.5) 在这里有效。请注意,这与 if swift(>=5.5) 不同,后者不一定有效,具体取决于您在项目中设置的 swift 版本。

Xcode 12.5   :      Swift version 5.4.2

Xcode 12.3   :      Swift version 5.3.2

Xcode 12.2   :      Swift version 5.3.1

Xcode 11.6   :      Swift version 5.2.4

Xcode 11.5   :      Swift version 5.2.4

Xcode 11.4   :      Swift version 5.2

Xcode 11.3   :      Swift version 5.1.3

Xcode 11.2.1 :      Swift version 5.1.2

Xcode 11.1   :      Swift version 5.1

然后是检查方法:

#if compiler(>=5) //4.2, 3.0, 2.0 replace whatever swift version you wants to check
print("Hello Swift 5")
#endif

祝一切顺利