使用不推荐的方法时在Xcode中显示自定义警告

使用不推荐的方法时在Xcode中显示自定义警告,xcode,llvm,Xcode,Llvm,重构旧代码时,我想更改以下方法: - (id)initWithFrame:(CGRect)frame { // original logic } 致: 但是,如果有人仍在使用该原始方法,我希望Xcode发出警告(类似于iOS中的方法被弃用时)。理想情况下,类似于: - (id)initWithFrame:(CGRect)frame __warning__(@"This method has been replaced to ensure that you set the delega

重构旧代码时,我想更改以下方法:

- (id)initWithFrame:(CGRect)frame
{
     // original logic
}
致:

但是,如果有人仍在使用该原始方法,我希望Xcode发出警告(类似于iOS中的方法被弃用时)。理想情况下,类似于:

- (id)initWithFrame:(CGRect)frame __warning__(@"This method has been replaced to ensure that you set the delegate. Please update your code.");
{
     return [self initWithFrame:frame andDelegate:nil];
}

请注意,这些弃用可能发生在新版本iOS发布之前。

在接口文件中,执行以下操作:

- (id)initWithFrame:(CGRect)frame__attribute__((deprecated("Use initWithFrame: andDelegate")));

为了完整起见,我还要补充一点,如果你想阻止某人使用某个特定的方法,你可以使用“不可用”标志,例如

- (id)initWithFrame:(CGRect)frame__attribute__((unavailable("Use initWithFrame: andDelegate")));
这会触发错误,而不是警告


您可以在

中找到更多信息,声明-(id)initWithFrame:(CGRect)frame_uuu属性(不推荐(“使用initWithFrame:andDelegate”);在界面中,它不工作??很好。是的。谢谢回答一下,我就接受。谢谢我以为你已经试过了!然而,作为一个答案张贴。
- (id)initWithFrame:(CGRect)frame__attribute__((deprecated("Use initWithFrame: andDelegate")));
- (id)initWithFrame:(CGRect)frame__attribute__((unavailable("Use initWithFrame: andDelegate")));