xcode - 使用已弃用的方法时在 Xcode 中显示自定义警告

标签 xcode llvm

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

- (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/

相关文章:

swift - 尝试使用 segues 将数据从一个 View Controller 传递到另一个 View Controller

llvm - 如何在 LLVM 机器函数传递中创建新的虚拟寄存器?

cmake - 在 CMake 中指定 CUDA 编译器

ios - 返回主视图时如何从内存中删除 subview ?

iphone - 我目前有一个加速计 Action ,可以显示图像但不会清除/重置

arrays - Xcode 在处理文件时卡住

objective-c - NSOperationQueue 异步下载网页

c - 在 macOS 上找不到 Homebrew 的 LLVM "fatal error: ' stdio.h' 文件

swift - 编程语言如何实现跨平台特性?

haskell - 选择与 LLVM 一起使用的函数式编程语言时,有哪些权衡?