【问题标题】:is there an easy way to disallow some method from framework?有没有一种简单的方法来禁止框架中的某些方法?
【发布时间】:2012-02-10 02:45:52
【问题描述】:

我想禁止某些方法。例如

+ (MPMusicPlayerController*)iPodMusicPlayer

所以我尝试这样做:

@interface MPMusicPlayerController (Disallowed)
// do never this method cause issues #957 #632 #1463
// read #632 description to detail analysis why code should never use this method while
// applicationMusicPlayer is used
+ (MPMusicPlayerController*)iPodMusicPlayer __attribute__((unavailable));
+ (MPMusicPlayerController*)iPodMusicPlayer __attribute__((deprecated));
@end

但是下面的代码还是编译了,没有任何警告

MPMusicPlayerController * curPlayer = [MPMusicPlayerController iPodMusicPlayer];

有什么想法吗?

【问题讨论】:

  • 我很难想象这样的情况是解决问题的合适方法。

标签: objective-c ios methods deprecated


【解决方案1】:

编译时解决方案:

一种方式,只需使用下一个代码:

#pragma GCC poison iPodMusicPlayer

我应该提到 SDK61 和 SDK7 不能“毒”包含“:”的选择器 原因llvm bug :(

另一种方式:

#import <MediaPlayer/MediaPlayer.h> // import original methods at first
@interface MPMusicPlayerController (Disallowed)
+ (MPMusicPlayerController*)disallowedMethod_iPodMusicPlayer __attribute__((unavailable));
@end

#define iPodMusicPlayer disallowedMethod_iPodMusicPlayer

【讨论】:

    【解决方案2】:

    我认为您可以在 MPMusicPlayerController 上创建一个类别,例如重写(MPMusicPlayerController+Override),然后重写 iPodMusicPlayer 类方法返回 nil。一定要#include MPMusicPlayerController+Override.h。

    您可以在您的方法中添加警告标记,以提醒任何人不要使用它:

    #warning Disabled method - do not use.
    

    如果这对你有用,请告诉我。

    达米安

    【讨论】:

    • 谢谢。此方法是运行时解决方案。但是编译时解决方案是首选。
    猜你喜欢
    • 1970-01-01
    • 2017-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多