如何在Xcode中确定Objective-C语言版本

5

有没有办法检查我在App中使用的Objective-C版本?


你是不是想说操作系统版本?你可以使用这个代码 [[UIDevice currentDevice] systemVersion] 来检查系统版本。 - Kampai
你为什么想要在Xcode中?你打算用它做什么? - Ekta Padaliya
请检查您在构建设置中使用的Objective-C编译器。 - KIDdAe
Objective-C 功能可用性指数 有帮助吗? - Ken Thomases
请参考以下链接以获取帮助:运行时版本和平台 - Anbu.Karthik
2个回答

6

Objective-C语言支持水平由编译代码所使用的Clang版本定义,而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检查Objective-C语言功能的可用性,而不是使用__clang_major____clang_minor__

有一些其他值得一提的历史悠久的语言功能,您甚至不必再测试其可用性:

  • NS_ENUMNS_OPTIONS在Xcode 4.5中添加
  • NSDictionaryNSArray下标在Xcode 4.4 / 4.5中添加
  • @YES@NO字面量在Xcode 4.4 / 4.5中添加
  • NSNumberNSDictionaryNSArray字面量在Xcode 4.4中添加
  • @autoreleasepool块在Xcode 4.2中添加
  • "Objective-C 2.0"非常古老(Xcode 2.x)

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

相关链接:


1

Objective-C运行时有两个版本——“现代”和“传统”。iPhone应用程序和OS X v10.5及更高版本的64位程序使用现代运行时。在这里查看链接

所有引入的主要功能都可以在此处查看链接


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接