How do I flag a function as being deprecated in an

2020-02-08 05:48发布

问题:

How do I flag a function as being deprecated in an iOS Objective-C header file?

I'm guessing there's just some keyword I can stick after the function somewhere?

I would like for a compiler warning to be generated should anyone try and use the deprecated function, similar to the behavior seen in Apple's APIs.

回答1:

Try appending an attribute to your method declaration:

- (void)fooBar __attribute__ ((deprecated));

Taken from here.



回答2:

Tim's answer will actually produce a compiler warning; the other versions are merely comments which have no effect w.r.t. the compiler.

If you look at /usr/include/AvailabilityMacros.h, you'll see how Apple does this. That header uses __attribute__((deprecated)) and __attribute__((unavailable)) depending on whether the API is present but deprecated, or has actually been removed from the OS.



回答3:

Instead of __attribute__((deprecated)), you can use use the macros defined in <cdefs.h>:

- (void)fooBar __deprecated;
// Or better:
- (void)fooBar __deprecated_msg("Use barFoo instead.");

Or you can use the macros defined in <AvailabilityMacros.h>:

- (void)fooBar DEPRECATED_ATTRIBUTE;
// Or better:
- (void)fooBar DEPRECATED_MSG_ATTRIBUTE("Use barFoo instead.");

If you use Objective-C, it makes no difference as you are going to use a modern compiler, so you can go for Apple short syntax __deprecated_msg(). But if you use C for cross-platform, then DEPRECATED_MSG_ATTRIBUTE() uses the optimal availability definitions (for instance, it supports GCC3.1).



回答4:

From Apple's SFAuthorization.h:

/*!
DEPRECATED: Use obtainWithRight:flags:error:
@method permitWithRight:flags:
@abstract Call permitWithRight to gain a right to have
          access to a privilege operation.
@param rightName The name of an authorization right.
@param flags Authorization flags.
*/
- (OSStatus)permitWithRight:(AuthorizationString)rightName
                      flags:(AuthorizationFlags)flags;

If you're not using an automated documentation builder I'd say something like this is enough:

- (void)doSomething;           /* DEPRECATED */


回答5:

You could also follow the HeaderDoc manual. Where this syntax is used:

/*!
 * @abstract Foo is good for bar.
 *
 * @deprecated in version 2.0
 */