Delegates: Difference between revisions

Content deleted Content added
Updated only the first D entry (I don't have Tango)
Line 854: Line 854:


=={{header|Objective-C}}==
=={{header|Objective-C}}==
Classic Objective-C
{{works with|Cocoa}}
{{works with|Cocoa}}
{{works with|GNUstep}}
{{works with|GNUstep}}
Line 930: Line 931:
NSLog(@"%d\n", [[a operation] isEqualToString:@"delegate implementation"]);
NSLog(@"%d\n", [[a operation] isEqualToString:@"delegate implementation"]);


return 0;
}</lang>

Objective-C 2.0, modern runtime, Automatic Reference Counting, Autosynthesize (LLVM 4.0+)
{{works with|Cocoa}}

<lang objc>#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;
return 0;
}</lang>
}</lang>