API_DEPRECATED宏中的平台问题

问题描述

在我的应用程序中,我想将某些类标记为已弃用的API。为此,我使用了宏API_DEPRECATED

  /*
     * API Deprecations
     *
     * Use to specify the release that a particular API became unavailable.
     *
     * Platform names:
     *   macos,ios,tvos,watchos
     *
     * Examples:
     *
     *    API_DEPRECATED("No longer supported",macos(10.4,10.8))
     *    API_DEPRECATED("No longer supported",10.8),ios(2.0,3.0),watchos(2.0,tvos(9.0,10.0))
     *
     *    API_DEPRECATED_WITH_REPLACEMENT("-setName:",tvos(10.0,10.4),ios(9.0,10.0))
     *    API_DEPRECATED_WITH_REPLACEMENT("SomeClassName",10.6),3.0))
     */

    #define API_DEPRECATED(...) __API_DEPRECATED_MSG_GET_MACRO(__VA_ARGS__,__API_DEPRECATED_MSG8,__API_DEPRECATED_MSG7,__API_DEPRECATED_MSG6,__API_DEPRECATED_MSG5,__API_DEPRECATED_MSG4,__API_DEPRECATED_MSG3,__API_DEPRECATED_MSG2,__API_DEPRECATED_MSG1,0)(__VA_ARGS__)

我以这种方式使用此宏:

API_DEPRECATED("UIFont",ios(7.0,API_TO_BE_DEPRECATED)) // here I have a warning: Unknown platform '__API_DEPRECATED_PLATFORM_' in availability macro

如何使用此宏将我的API标记为已弃用?

UPD API_TO_BE_DEPRECATED在可用性中定义。h:

/* 
 * API_TO_BE_DEPRECATED is used as a version number in API that will be deprecated 
 * in an upcoming release. This soft deprecation is an intermediate step before formal 
 * deprecation to notify developers about the API before compiler warnings are generated.
 * You can find all places in your code that use soft deprecated API by redefining the 
 * value of this macro to your current minimum deployment target,for example:
 * (macOS)
 *   clang -DAPI_TO_BE_DEPRECATED=10.12 <other compiler flags>
 * (iOS)
 *   clang -DAPI_TO_BE_DEPRECATED=11.0 <other compiler flags>
 */
 
#ifndef API_TO_BE_DEPRECATED
#define API_TO_BE_DEPRECATED 100000

我找到了NS_CLASS_DEPRECATED_IOS,但是此宏仅适用于iOS。我在项目中使用了它,但想找到一些通用的解决方案:

#define MARK_CLASS_DEPRECATED(MSG) NS_CLASS_DEPRECATED_IOS(3_0,10_0,MSG)
#define MARK_METHOD_DEPRECATED(MSG) NS_DEPRECATED_IOS(3_0,MSG)

解决方法

使类仅从某些特定的OS版本开始可用。

API_AVAILABLE(ios(13.0))
@interface SceneDelegate : UIResponder <UIWindowSceneDelegate>
@property (strong,nonatomic) UIWindow * window;
@end

仅提供特定版本的资源,并提示要使用的其他内容

@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong,nonatomic) UIWindow *window
API_DEPRECATED_WITH_REPLACEMENT("SceneDelegate:window",ios(2.0,13.0)) NS_DEPRECATED_IOS(2.0,13.0);
...

将属性定义为已弃用,因此,在未警告开始特定操作系统版本的情况下不能使用该属性。

...
@property (strong,nonatomic) ExternalDisplay *externalDisplay
NS_DEPRECATED_IOS(2.0,13.0);
@end

定义从特定OS版本开始可用的方法实现

- (void)application:(UIApplication *)application didDiscardSceneSessions:(NSSet<UISceneSession *> *)sceneSessions API_AVAILABLE(ios(13.0)) {
    // your code
}

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...