主页 How to Deprecate a Method in Objective-C
Post
Cancel

How to Deprecate a Method in Objective-C

Preface

Recently I’ve been working on SDK development. Sometimes you can’t easily remove an API because some people are still using it. So to keep the relevant method while marking it as deprecated, I use the following code:

1
__attribute__((deprecated("此方法已弃用,请使用xxxxx:方法")));

Scenario 1

I want to mark a method as deprecated and suggest passing a parameter in another way.

For example, in a controller, I want to indicate that setting a URL can be done directly via the setter method.

1
2
3
4
5
6
7
@interface VideoEditorViewController : UIViewController

@property(nonatomic, strong)NSURL *videoPath;

-(instancetype)initWithUrl:(NSURL *)path __attribute__((deprecated("使用setVideoPath:方法传入")));

@end

This way, calling it shows a warning directly, telling you that passing a URL via this method is deprecated.

For more uses of __attribute__, please refer to Apple’s official documentation and other blogs. I’ll keep updating with more uses in the future.

End of article

Reference

该博客文章由作者通过 CC BY 4.0 进行授权。