gpt4 book ai didi

ios - 没有冲突类别方法的警告

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:41:20 26 4
gpt4 key购买 nike

我正在运行 XCode 4.6.3 并尝试创建一个具有冲突类别方法的项目,如下所示。

//NSString+category1.h
@interface NSString (category1)

-(NSString*)foo;

@end

//NSString+category1.m
#import "NSString+category1.h"

@implementation NSString (category1)

-(NSString*)foo{
return self;
}

@end


// NSString+category2.h
@interface NSString (category2)

-(NSString*)foo;

@end

//NSString+category2.m
#import "NSString+category2.h"

@implementation NSString (category2)

-(NSString*)foo{
return self;
}

@end

XCode 4.6 应该会给我一些警告,但事实并非如此。是否需要设置特殊的编译器/链接器标志?

最佳答案

根据 Avoid Category Method Name Clashes使用 Objective-C 编程指南中:

If the name of a method declared in a category is the same as a method in the original class, or a method in another category on the same class (or even a superclass), the behavior is undefined as to which method implementation is used at runtime. ...

... In order to avoid undefined behavior, it’s best practice to add a prefix to method names in categories on framework classes, just like you should add a prefix to the names of your own classes. You might choose to use the same three letters you use for your class prefixes, but lowercase to follow the usual convention for method names, then an underscore, before the rest of the method name.

最重要的是,我相信开发人员有责任确保方法名称是唯一的。

关于ios - 没有冲突类别方法的警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19190821/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com