Problem with a platform in API_DEPRECATED macro

1.3k Views Asked by At

In my application I want to mark some class as a deprecated API. To achieve this I use macro 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", macos(10.4, 10.8), ios(2.0, 3.0), watchos(2.0, 3.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", macos(10.4, 10.6), watchos(2.0, 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__)

I use this macro in this way:

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

How I can use this macro to mark my API as deprecated?

UPD API_TO_BE_DEPRECATED is defined in availability.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

I've found NS_CLASS_DEPRECATED_IOS, but this macro only for iOS. I use it in my project, but want to find some universal solution:

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

There are 1 best solutions below

6
On

Make a Class only available starting with some specific OS Version.

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

Make a Property available only up to a specific Version and give a hint what else to use

@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);
...

Define a property as deprecated so it can not be used without warning beginning a specific OS Version.

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

Define a method implementation being use-able starting a specific OS Version

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