我想知道,相当于 C# 中的协议和委托。
所以这是我的协议,接口和定义协议的类的实现以及符合协议的类的实现。我想知道这在 c# 中的等价物。请 :)
/******************************************/
// Communicator Protocol
@class Communicator
@protocol CommunicatorDelegate <NSObject>
@required
- (void)communicator:(Communicator *)communicator receivedData:(NSData *)data;
- (void)fetchingFailedWithError:(NSError *)error;
@optional
- (void)anOptinalMethod;
@end
/******************************************/
// Communicator Class
@protocol CommunicatorDelegate;
@interface Communicator : NSObject
@property (weak, nonatomic) id<CommunicatorDelegate> delegate;
@end
/******************************************/
// Communicator Implementation
@implementation
-(void)someMethodThatFail:(NSError *)error;
{
[self.delegate fetchingFailedWithError:error];
}
- (void)someMethodThatGetData:(NSData *)data;
{
[self.delegate communicator:self receivedData:data];
}
@end
/******************************************/
// Interface of Some Class that conform with the protocol
#import "Communicator.h"
@interface SomeClass : NSObject <CommunicatorDelegate>
@end
/******************************************/
// Implementation of Some Class that conform with the protocol
- (void)communicator:(Communicator *)communicator receivedData:(NSData *)data;
{
// Do something
}
- (void)fetchingFailedWithError:(NSError *)error;
{
// Do something
}