相关疑难解决方法(0)

如果两个ObjC类别覆盖相同的方法会发生什么?

我知道有关Objective-C类别的一些规则:

  1. 类别方法不应覆盖现有方法(类或实例)
  2. 对同一个类实现相同方法的两个不同类别将导致未定义的行为

我想知道当我覆盖同一类别中我自己的一个类别方法时会发生什么.例如:

@interface NSView (MyExtensions)
- (void)foo; // NSView category implementation
@end

@interface MyClass : NSView
{ }
@end

@interface MyClass (MyExtensions)
- (void)foo; // MyClass category implementation
@end
Run Code Online (Sandbox Code Playgroud)

定义了这些接口后,当我运行以下代码时将执行哪个方法?

MyClass * instance = [[MyClass alloc] initWith...];
[instance foo];
[instance release];
Run Code Online (Sandbox Code Playgroud)

注意:使用我的编译器,MyClass实现优先,但我不确定是否可以保证发生,或者只是一种特定的未定义行为.

methods inheritance cocoa objective-c categories

13
推荐指数
2
解决办法
6070
查看次数

标签 统计

categories ×1

cocoa ×1

inheritance ×1

methods ×1

objective-c ×1