在 Objective-C 中,如何防止类别方法中的名称冲突? [英] In Objective-C, how to prevent name collision in category methods?
问题描述
有些人使用前缀来命名他们的类别方法,以防止与其他外部代码可能发生的名称冲突,但我个人发现在其他清晰的名称中添加噪音.有没有比添加前缀更好的方法来避免类别名称冲突?
Some people have used prefixes to name their category methods in order to prevent possible name collisions with other external code, but I personally find that adding noise to a name that is otherwise clear. Is there a better way to avoid category name collisions than prefixing it?
例如
@interface UILabel (Extras)
-(void)prefix_extraMethod;
@end
推荐答案
Objective-C 中不存在命名空间,因此唯一的方法是为您的方法添加前缀.
In Objective-C doesn't exist namespace so the only way is to prefix your method.
如果不想添加前缀(我同意你的看法......这很丑陋)你可以将 OBJC_PRINT_REPLACED_METHODS
环境变量设置为 YES
以在你使用方法名称时收到警告与其他现有方法发生冲突.
If don't want to prefix (I agree with you..it's ugly) you can the OBJC_PRINT_REPLACED_METHODS
environment variable to YES
to receive a warning if you method name collide with other existing method.
这篇关于在 Objective-C 中,如何防止类别方法中的名称冲突?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!