重构旧代码,我想更改以下方法:
- (id)initWithFrame:(CGRect)frame
{
// original logic
}
至:
- (id)initWithFrame:(CGRect)frame andDelegate:(id<myDelegateProtocol>)delegate
{
// original logic
if(delegate)
{
_delegate = delegate;
}
}
为了确保没有依赖代码中断,我更新了原始方法以引用旧方法,如下所示:
- (id)initWithFrame:(CGRect)frame
{
return [self initWithFrame:frame andDelegate:nil];
}
但是,如果有人仍在使用原始方法,我希望 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 发布之前发生。
最佳答案
在接口(interface)文件中,执行以下操作:
- (id)initWithFrame:(CGRect)frame__attribute__((deprecated("Use initWithFrame: andDelegate")));
关于xcode - 使用已弃用的方法时在 Xcode 中显示自定义警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14812177/