有什么方法可以检查我在App中使用的Objective-C版本

最佳答案

Objective-C语言支持级别由用于编译代码的Clang版本定义,该版本本身与Xcode版本非常接近。

#if __clang_major__ >= 11
    NSLog(@"My Objective-C language support is what Apple Clang/Xcode 11.x can support.");
    // This language version supports the additional features/fixes written under "Apple Clang Compiler"
    // https://developer.apple.com/documentation/xcode_release_notes/xcode_11_release_notes
    // Notably this version adds Objective-C support to:
    // - `[[clang::no_destroy]]` and `[[clang::always_destroy]]`

#elif __clang_major__ >= 10
    NSLog(@"My Objective-C language support is what Apple Clang/Xcode 10.x can support.");
    // This language version supports the additional features/fixes written under "Apple Clang Compiler"
    // https://developer.apple.com/documentation/xcode_release_notes/xcode_10_release_notes
    // Notably this version adds macro support to:
    // - detect most builtin pseudo-functions with `__has_builtin`

#elif __clang_major__ >= 9
    NSLog(@"My Objective-C language support is what Apple Clang/Xcode 9.x can support.");
    // This language version supports the additional features/fixes written under "Apple LLVM Compiler and Low-Level Tools"
    // https://developer.apple.com/library/archive/releasenotes/DeveloperTools/RN-Xcode/Chapters/Introduction.html#//apple_ref/doc/uid/TP40001051-CH1-SW878
    // Notably this version adds Objective-C support for:
    // - the `@available` language feature

#elif __clang_major__ >= 8
    NSLog(@"My Objective-C language support is what Apple Clang/Xcode 8.x can support.");
    // This language version supports the additional features/fixes written under "Objective-C and C++"
    // https://developer.apple.com/library/archive/releasenotes/DeveloperTools/RN-Xcode/Chapters/Introduction.html#//apple_ref/doc/uid/TP40001051-CH1-SW78
    // Notably this version adds Objective-C support for:
    // - the `@property (class)` language feature

#elif __clang_major__ >= 7
    NSLog(@"My Objective-C language support is what Apple Clang/Xcode 7.x can support.");
    // This language version supports the additional features/fixes written under "Objective-C"
    // https://developer.apple.com/library/archive/releasenotes/DeveloperTools/RN-Xcode/Chapters/Introduction.html#//apple_ref/doc/uid/TP40001051-CH1-SW326
    // Notably this version adds Objective-C support for:
    // - `CF_RETURNS_NOT_RETAINED` and `CF_RETURNS_RETAINED`
    // - `__kindof`
    // - `_Nullable`, `_Nonnull`, and `_Null_unspecified`
    // - Lightweight generics like `NSArray<UIImage *> *` and `NSDictionary<NSString *, NSURL *>`

#elif __clang_major__ >= 6
    NSLog(@"My Objective-C language support is what Apple Clang/Xcode 6.x can support.");
    // This language version supports the additional features/fixes written at:
    // https://developer.apple.com/library/archive/releasenotes/DeveloperTools/RN-Xcode/Chapters/Introduction.html#//apple_ref/doc/uid/TP40001051-CH1-SW453

#else
    NSLog(@"My Objective-C language support is so old that I won't even be allowed to publish this on any App Store nowadays.");

#endif

如果您需要更精确的版本,也可以使用__clang_minor__

建议尽可能使用 __has_builtin 而不是__clang_major____clang_minor__来检查Objective-C语言功能的可用性。

一些其他较古老的历史语言功能,您甚至不必再测试可用性:

在Xcode 4.5中添加了
  • NS_ENUMNS_OPTIONS
  • 在Xcode 4.4/4.5 中添加了
  • NSDictionaryNSArray下标
    在Xcode 4.4/4.5
  • 中添加了
  • @YES@NO文字
    在Xcode 4.4
  • 中添加了
  • NSNumberNSDictionaryNSArray文字
    在Xcode 4.2中添加了
  • @autoreleasepool
  • “Objective-C 2.0”非常旧(Xcode 2.x)

  • 最后,“现代Objective-C”仅指当前对Objective-C的任何Xcode支持。

    有关的:
  • How can I reliably detect the version of clang at preprocessing time?
  • 10-08 09:31
    查看更多