3 回答

TA貢獻1827條經驗 獲得超4個贊
至少在OS X上,使用最新的clang / SDK,現在有一個-Wpartial-availability選項(例如在“其他警告選項”中添加),然后可以定義以下宏以封裝用于支持運行時測試的代碼(如果支持該方法)
#define START_IGNORE_PARTIAL _Pragma("clang diagnostic push") _Pragma("clang diagnostic ignored \"-Wpartial-availability\"")
#define END_IGNORE_PARTIAL _Pragma("clang diagnostic pop")
我還沒有在iOS上進行測試。

TA貢獻1898條經驗 獲得超8個贊
深入研究之后AvailabilityInternal.h,我意識到部署目標上方的所有可用版本都被__AVAILABILITY_INTERNAL_WEAK_IMPORT宏標記。
因此,我可以通過重新定義該宏來生成警告:
#import <Availability.h>
#undef __AVAILABILITY_INTERNAL_WEAK_IMPORT
#define __AVAILABILITY_INTERNAL_WEAK_IMPORT \
__attribute__((weak_import,deprecated("API newer than Deployment Target.")))
通過將此代碼放置在項目的預編譯頭文件中,任何可能在受支持的最低iOS版本上導致崩潰的API用法現在都會產生警告。如果您正確地保護了呼叫,則可以專門針對該呼叫禁用警告(Apple SDK兼容性指南中的修改后的示例):
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
if ([UIPrintInteractionController class]) {
// Create an instance of the class and use it.
}
#pragma GCC diagnostic warning "-Wdeprecated-declarations"
else {
// Alternate code path to follow when the
// class is not available.
}
- 3 回答
- 0 關注
- 773 瀏覽
添加回答
舉報