Xcode 有没有办法警告新的 API 调用? [英] Is there a way for Xcode to warn about new API calls?

查看:33
本文介绍了Xcode 有没有办法警告新的 API 调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不止一次看到 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆