有没有办法检查我在App中使用的Objective-C版本?
有没有办法检查我在App中使用的Objective-C版本?
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_ENUM
和NS_OPTIONS
在Xcode 4.5中添加NSDictionary
和NSArray
下标在Xcode 4.4 / 4.5中添加@YES
和@NO
字面量在Xcode 4.4 / 4.5中添加NSNumber
、NSDictionary
和NSArray
字面量在Xcode 4.4中添加@autoreleasepool
块在Xcode 4.2中添加最后,“现代Objective-C”仅指任何当前可用的Xcode支持Objective-C。
相关链接:
[[UIDevice currentDevice] systemVersion]
来检查系统版本。 - Kampai