Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
45
Task/Delegates/Objective-C/delegates-2.m
Normal file
45
Task/Delegates/Objective-C/delegates-2.m
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
#import <Foundation/Foundation.h>
|
||||
|
||||
// Formal protocol for the delegate
|
||||
@protocol DelegatorDelegatingProtocol
|
||||
- (NSString *)thing;
|
||||
@end
|
||||
|
||||
@interface Delegator : NSObject
|
||||
@property (weak) id delegate;
|
||||
- (NSString *)operation;
|
||||
@end
|
||||
@implementation Delegator
|
||||
- (NSString *)operation {
|
||||
if ([self.delegate respondsToSelector: @selector(thing)])
|
||||
return [self.delegate thing];
|
||||
|
||||
return @"default implementation";
|
||||
}
|
||||
@end
|
||||
|
||||
@interface Delegate : NSObject
|
||||
<DelegatorDelegatingProtocol>
|
||||
@end
|
||||
@implementation Delegate
|
||||
- (NSString *)thing { return @"delegate implementation"; }
|
||||
@end
|
||||
|
||||
// Example usage with Automatic Reference Counting
|
||||
int main() {
|
||||
@autoreleasepool {
|
||||
// Without a delegate:
|
||||
Delegator *a = [Delegator new];
|
||||
NSLog(@"%@", [a operation]); // prints "default implementation"
|
||||
|
||||
// With a delegate that does not implement thing:
|
||||
a.delegate = @"A delegate may be any object";
|
||||
NSLog(@"%@", [a operation]); // prints "default implementation"
|
||||
|
||||
// With a delegate that implements "thing":
|
||||
Delegate *d = [Delegate new];
|
||||
a.delegate = d;
|
||||
NSLog(@"%@", [a operation]); // prints "delegate implementation"
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue