获取 Xcode 5 以警告新的 API 调用 [英] Get Xcode 5 to warn about new API calls

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

问题描述

Xcode 4 有一种方法.x:

#define __AVAILABILITY_TOO_NEW __attribute__((deprecated("TOO NEW!"))) __attribute__((weak_import))

#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_6_0
#undef __AVAILABILITY_INTERNAL__IPHONE_6_0
#define __AVAILABILITY_INTERNAL__IPHONE_6_0 __AVAILABILITY_TOO_NEW
#endif

但这不再起作用,因为 iOS 7 SDK 可用性宏已更改,现在有更多差异和选项:

But that doesn't work anymore because the iOS 7 SDK Availability macros have changed and now have more variances and options:

iOS 6 SDK 的 AvailabilityInternal.h:

iOS 6 SDK's AvailabilityInternal.h:

#define __AVAILABILITY_INTERNAL__IPHONE_6_0                    __attribute__((availability(ios,introduced=6.0)))
#define __AVAILABILITY_INTERNAL__IPHONE_6_0_DEP__IPHONE_6_0    __attribute__((availability(ios,introduced=6.0,deprecated=6.0)))
#define __AVAILABILITY_INTERNAL__IPHONE_6_0_DEP__IPHONE_6_1    __attribute__((availability(ios,introduced=6.0,deprecated=6.1)))
#define __AVAILABILITY_INTERNAL__IPHONE_6_0_DEP__IPHONE_NA     __attribute__((availability(ios,introduced=6.0)))

iOS 7 SDK:

#define __AVAILABILITY_INTERNAL__IPHONE_6_0                    __attribute__((availability(ios,introduced=6.0)))
#define __AVAILABILITY_INTERNAL__IPHONE_6_0_DEP__IPHONE_6_0    __attribute__((availability(ios,introduced=6.0,deprecated=6.0)))
#if __has_feature(attribute_availability_with_message)
     #define __AVAILABILITY_INTERNAL__IPHONE_6_0_DEP__IPHONE_6_0_MSG(_msg)    __attribute__((availability(ios,introduced=6.0,deprecated=6.0,message=_msg)))
#else
     #define __AVAILABILITY_INTERNAL__IPHONE_6_0_DEP__IPHONE_6_0_MSG(_msg)    __attribute__((availability(ios,introduced=6.0,deprecated=6.0)))
#endif
#define __AVAILABILITY_INTERNAL__IPHONE_6_0_DEP__IPHONE_6_1    __attribute__((availability(ios,introduced=6.0,deprecated=6.1)))
#if __has_feature(attribute_availability_with_message)
     #define __AVAILABILITY_INTERNAL__IPHONE_6_0_DEP__IPHONE_6_1_MSG(_msg)    __attribute__((availability(ios,introduced=6.0,deprecated=6.1,message=_msg)))
#else
     #define __AVAILABILITY_INTERNAL__IPHONE_6_0_DEP__IPHONE_6_1_MSG(_msg)    __attribute__((availability(ios,introduced=6.0,deprecated=6.1)))
#endif
#define __AVAILABILITY_INTERNAL__IPHONE_6_0_DEP__IPHONE_7_0    __attribute__((availability(ios,introduced=6.0,deprecated=7.0)))
#if __has_feature(attribute_availability_with_message)
     #define __AVAILABILITY_INTERNAL__IPHONE_6_0_DEP__IPHONE_7_0_MSG(_msg)    __attribute__((availability(ios,introduced=6.0,deprecated=7.0,message=_msg)))
#else
     #define __AVAILABILITY_INTERNAL__IPHONE_6_0_DEP__IPHONE_7_0_MSG(_msg)    __attribute__((availability(ios,introduced=6.0,deprecated=7.0)))
#endif
#define __AVAILABILITY_INTERNAL__IPHONE_6_0_DEP__IPHONE_NA               __attribute__((availability(ios,introduced=6.0)))
#define __AVAILABILITY_INTERNAL__IPHONE_6_0_DEP__IPHONE_NA_MSG(_msg)     __attribute__((availability(ios,introduced=6.0)))

是不是 Xcode 5 有 attribute_availability_with_message 功能,所以旧的宏重新定义不再使用?

Could it be that Xcode 5 has the attribute_availability_with_message feature so the old macro redefinition is no longer used?

此外,打印比太新!"更多的东西会很酷.包含所有新引入/弃用信息的消息.

Also it would be cool to print more than the "TOO NEW!" message with all the new introduced/deprecated information.

iOS 7 SDK 中的大多数定义从 __OSX_AVAILABLE_STARTING(_ios)/__AVAILABILITY_INTERNAL##_ios 移动到 NS_AVAILABLE_IOS(_ios)/CF_AVAILABLE_IOS(_ios),所以重新定义:

Most definitions in iOS 7 SDK moved from __OSX_AVAILABLE_STARTING(_ios)/__AVAILABILITY_INTERNAL##_ios to NS_AVAILABLE_IOS(_ios)/CF_AVAILABLE_IOS(_ios), so redefining:

#undef NS_AVAILABLE_IOS
#define NS_AVAILABLE_IOS(_ios) __attribute__((availability(ios,__NSi_##_ios))) __attribute__((deprecated("TOO NEW!")))

应该可以.实际上它确实如此,因为 Xcode 5 自动完成功能将这些方法显示为已弃用.

Should work. And actually it does because Xcode 5 autocompletion shows the methods as deprecated.

尽管具有 GCC_WARN_ABOUT_DEPRECATED_FUNCTIONS 构建选项,但构建不会触发任何警告...

Building however doesn't trigger any warning despite having the GCC_WARN_ABOUT_DEPRECATED_FUNCTIONS build option on...

推荐答案

从 Xcode 5.0 开始,无法通过重新定义宏来从新的 API 调用中获得警告.

As of Xcode 5.0 it is not possible to get warnings from new API calls by redefining macros.

重新定义宏确实会出现在自动完成和预处理预览中(Assistant Editor > Preprocess),但尽管使用了 deprecateunavailable 关键字,但不会触发警告.

Redefining a macro does show up in autocompletion and preprocessed preview (Assistant Editor > Preprocess) but does not trigger a warning despite using deprecate or unavailable keywords.

Xcode 5.0.1+ 确实显示了警告,因此 __AVAILABILITY_INTERNAL__IPHONE_X_XNS_AVAILABLE_IOS 现在都可以按照问题和@borrrden 的答案重新定义.

Xcode 5.0.1+ does show a warning so both __AVAILABILITY_INTERNAL__IPHONE_X_X and NS_AVAILABLE_IOS can now be redefined as mentioned in the question and @borrrden answer.

代码可用作 Gist:

#define __IPHONE_OS_VERSION_SOFT_MAX_REQUIRED __IPHONE_5_0

#ifndef __IPHONE_OS_VERSION_SOFT_MAX_REQUIRED
    #define __IPHONE_OS_VERSION_SOFT_MAX_REQUIRED __IPHONE_OS_VERSION_MIN_REQUIRED
#endif

#if __IPHONE_OS_VERSION_SOFT_MAX_REQUIRED < __IPHONE_OS_VERSION_MIN_REQUIRED
    #error You cannot ask for a soft max version which is less than the deployment target
#endif

#define __NBU_AVAILABILITY_STARTING(version) __attribute__((deprecated("Only available in iOS " version "+"))) __attribute__((weak_import))

#if __IPHONE_OS_VERSION_SOFT_MAX_REQUIRED < __IPHONE_2_0
    #undef  __AVAILABILITY_INTERNAL__IPHONE_2_0
    #define __AVAILABILITY_INTERNAL__IPHONE_2_0 __NBU_AVAILABILITY_STARTING("2.0")
    #define __NBU_APICHECK_2_0(_ios)            __NBU_AVAILABILITY_STARTING("2.0")
#else
    #define __NBU_APICHECK_2_0(_ios)            CF_AVAILABLE_IOS(_ios)
#endif

#if __IPHONE_OS_VERSION_SOFT_MAX_REQUIRED < __IPHONE_2_1
    #undef  __AVAILABILITY_INTERNAL__IPHONE_2_1
    #define __AVAILABILITY_INTERNAL__IPHONE_2_1 __NBU_AVAILABILITY_STARTING("2.1")
    #define __NBU_APICHECK_2_1(_ios)            __NBU_AVAILABILITY_STARTING("2.1")
#else
    #define __NBU_APICHECK_2_1(_ios)            CF_AVAILABLE_IOS(_ios)
#endif

//...

#if __IPHONE_OS_VERSION_SOFT_MAX_REQUIRED < __IPHONE_7_0
    #undef  __AVAILABILITY_INTERNAL__IPHONE_7_0
    #define __AVAILABILITY_INTERNAL__IPHONE_7_0 __NBU_AVAILABILITY_STARTING("7.0")
    #define __NBU_APICHECK_7_0(_ios)            __NBU_AVAILABILITY_STARTING("7.0")
#else
    #define __NBU_APICHECK_7_0(_ios)            CF_AVAILABLE_IOS(_ios)
#endif

#undef  NS_AVAILABLE_IOS
#define NS_AVAILABLE_IOS(_ios)                  __NBU_APICHECK_##_ios( _ios )

#undef  __OSX_AVAILABLE_BUT_DEPRECATED
#define __OSX_AVAILABLE_BUT_DEPRECATED(_osx, _osxDep, _ios, _iosDep)            __AVAILABILITY_INTERNAL##_ios

#undef  __OSX_AVAILABLE_BUT_DEPRECATED_MSG
#define __OSX_AVAILABLE_BUT_DEPRECATED_MSG(_osx, _osxDep, _ios, _iosDep, _msg)  __AVAILABILITY_INTERNAL##_ios

这篇关于获取 Xcode 5 以警告新的 API 调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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