Yoon Lee
Yoon Lee

Reputation: 2039

Delegation question

code structure

@protocal A_Delegate
{
     -(void)doIt:(BOOL)isDone;
}

Super Class A // has properties of set delegate
-(void) setDelegate:(id<A_Delegate>)_delegate
{
       /*self.delegate = _delegate*/ error, compiler stops right there and doesn't assigns the value from '_delegate'

self.delegate = _delegate.

       //should be
       delegate = _delegate;
}

Sub Class B : A // want to call and define the delegation for the super class of B which is A
-(void) acquireDelegation:(id<A_Delegate>)_delegate
{
       [[super delegate] setDelegate];
}

Now, the Class C want to use the Class B and want to know its state,

Class C : NSObject <A_Delegation>
-(void) doSomething
{
     B *b = [[B alloc]init];
     [b aquireDelegation:self];
}
-(void)doIt:(BOOL)isDone
{
      if(isDone)
      // Do Something
}

Does any body know What I have done wrong and why super can't delegation? Is it possible to fix?

resolved.

Upvotes: 0

Views: 363

Answers (2)

Simon Goldeen
Simon Goldeen

Reputation: 9080

There are a couple issues. First, that protocol definition, well, isn't one. The syntax is all wrong. Secondly, what is "Super Class A" is that meant to be a comment?

Anyhoo, the major problem is in the setDelegate: method. If you have defined the delegate property then the line:

self.delegate = _delegate;

is equal to the line:

[self setDelegate:_delegate];

So, in the method -setDelegate you are calling -setDelegate.

Upvotes: 0

Max
Max

Reputation: 16709

This would lead to infinite loop (until the stack overflow):

-(void) setDelegate:(id<A_Delegate>)_delegate
{
       self.delegate = _delegate; // error, compiler stops right there and doesn't assigns the value from '_delegate'
}

cause self.delegate = _delegate; is calling setDelegate:. You have to assign to the ivar itself.

And I think this is not compiler. It is everything in runtime...

Upvotes: 2

Related Questions