Delegates: Difference between revisions

Content deleted Content added
Typos
Example of delegate that does not implement "thing"
Line 12: Line 12:
* Implement "thing" and return the string "delegate implementaion"
* Implement "thing" and return the string "delegate implementaion"


Show how objets are created and used. First, without a delegate, then with a delegate.
Show how objets are created and used. First, without a delegate, then with a delegate that does not implement 'thing', and last with a delegate that implement "thing".


==[[Objective-C]]==
==[[Objective-C]]==
Line 64: Line 64:
int main()
int main()
{
{
// Without a delegate
// Without a delegate:
Delegator *a = [[Delegator alloc] init];
Delegator *a = [[Delegator alloc] init];
assert([[a operation] isEqualToString:@"default implementation"]);
assert([[a operation] isEqualToString:@"default implementation"]);


// With a delegate
// With a delegate that does not implement thing:
[a setDelegate:@"A delegate may be any object"];
assert([isEqualToString:@"delegate implementation"]);

// With a delegate that implement "thing":
Delegate *d = [[Delegate alloc] init];
Delegate *d = [[Delegate alloc] init];
[a setDelegate:d];
[a setDelegate:d];
Line 99: Line 103:
assert a.operation() == 'default implementation'
assert a.operation() == 'default implementation'


# With delegate
# With a delegate that does not implement "thing"
a.delegate = 'A delegate may be any object'
assert a.operation() == 'default implementation'

# With delegate that implement "thing"
a.delegate = Delegate()
a.delegate = Delegate()
assert a.operation() == 'delegate implementation'
assert a.operation() == 'delegate implementation'