如何向Cocoa中的现有协议添加方法? [英] How to add a method to an existing protocol in Cocoa?
问题描述
我想在现有协议中扩展或添加其他方法。虽然协议特别不重要,这是我想要做的。
@protocol NSMatrixDelegate
- (void)myNewMethod:(id)sender;
@end
编译器警告我有一个相同协议的重复声明。
感谢。
无法定义协议的类别。有两种方法:
- 使用新的正式协议
- 使用非正式协议运行时检查
正式协议
定义一个新的正式协议this:
@protocol MyCustomMatrixDelegate< NSMatrixDelegate>
- (void)myNewMethod:(id)sender;
@end
然后你会使你的自定义类符合< MyCustomMatrixDelegate>
而不是< NSMatrixDelegate>
。如果你使用这种方法,有一些需要注意的事情: [self delegate]
可能会被声明为 id< NSMatrixDelegate>
。这意味着你不能做 [[self delegate] myNewMethod:obj]
,因为< NSMatrixDelegate>
不会声明 myNewMethod:
方法。
方法是重新输入
对象。可能类似:
- (id< MyCustomMatrixDelegate>)customDelegate {
return(id< MyCustomMatrixDelegate>)[self代表];
}
(但是,您可能需要先进行一些类型检查, / p>
if([[self delegate] conformsToProtocol:@protocol(MyCustomMatrixDelegate)]){
return(id< MyCustomMatrixDelegate& )[self delegate];
}
return nil;
p>
然后你会:
[[self customDelegate] myNewMethod :obj];
非正式协议
NSObject(MyCustomMatrixDelegate)的
:
$ b
)
- (void)myNewMethod:(id)sender;
@end
那么你只是不实现该方法。在您发送方法的类中,您将执行:
if([[self delegate] responsesToSelector:@selector myNewMethod :)]){
[[self delegate] myNewMethod:someSenderValue];
}
I want to extend or add another method to an existing protocol. Although the protocol in particular is not important, this is what I am trying to do.
@protocol NSMatrixDelegate
- (void)myNewMethod:(id)sender;
@end
The compiler warns that I have a duplicate declaration of the same protocol. How would I do this properly?
Thanks.
You can't define categories for protocols. There are 2 ways around this:
- use a new formal protocol
- use an informal protocol and runtime checking
Formal Protocol
Defining a new formal protocol would look like this:
@protocol MyCustomMatrixDelegate <NSMatrixDelegate>
- (void) myNewMethod:(id)sender;
@end
Then you would make your custom class conform to <MyCustomMatrixDelegate>
instead of <NSMatrixDelegate>
. If you use this approach, there's something to be aware of: [self delegate]
will likely be declared as id<NSMatrixDelegate>
. This means that you can't do [[self delegate] myNewMethod:obj]
, because <NSMatrixDelegate>
does not declare the myNewMethod:
method.
The way around this is to retype the delegate
object via casting. Maybe something like:
- (id<MyCustomMatrixDelegate>) customDelegate {
return (id<MyCustomMatrixDelegate>)[self delegate];
}
(However, you might want to do some type checking first, like:
if ([[self delegate] conformsToProtocol:@protocol(MyCustomMatrixDelegate)]) {
return (id<MyCustomMatrixDelegate>)[self delegate];
}
return nil;
)
And then you'd do:
[[self customDelegate] myNewMethod:obj];
Informal Protocol
This is really a fancy name for a category on NSObject
:
@interface NSObject (MyCustomMatrixDelegate)
- (void) myNewMethod:(id)sender;
@end
Then you just don't implement the method. In your class that would send the method, you'd do:
if ([[self delegate] respondsToSelector:@selector(myNewMethod:)]) {
[[self delegate] myNewMethod:someSenderValue];
}
这篇关于如何向Cocoa中的现有协议添加方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!