有没有办法让XCode警告新的API调用? [英] Is there a way for XCode to warn about new API calls?
问题描述
我不止一次看到崩溃的错误出现在iOS 3.x上,原因是使用了4.x中引入的新调用而没有经过适当的检查。
On more than one occasion I've seen crashing bugs appear on iOS 3.x due to use of a new call that was introduced in 4.x without proper checking.
有没有办法让XCode警告只有比部署目标更新版本的类,方法和程序?
Is there a way for XCode to warn about classes, methods and procedures that are only available a later version than the deployment target?
这样我就可以轻松列出通过所有代码并确保它被正确地条件化。
That way I could easily list through all the code and make sure it's properly conditionalized.
推荐答案
在挖掘 AvailabilityInternal.h
,我意识到上述所有可用版本部署目标使用 __ AVAILABILITY_INTERNAL_WEAK_IMPORT
宏进行标记。
因此,我可以通过重新定义该宏来生成警告:
Therefore, I can generate warnings by redefining that macro:
#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兼容性指南):
By placing this code in a project's precompiled header, any use of an API that might cause a crash on the lowest supported iOS version now generates a warning. If you correctly guard the call, you can disable the warning specifically for that call (modified exmaple from Apple's SDK Compatibility Guide):
#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.
}
这篇关于有没有办法让XCode警告新的API调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!