I have a class object that i’m using a lot from different places.
Now i’m using the class like this:
myClass.delegate = self;
[myClass doSomething];
doSomething creates a new class object that calculate stuff and can take up to 1 min before it sends back a result to the delegate like this:
-(void)doSomething {
CalculateStuff *calc = [[calculateStuff alloc] init];
calc.delegate = self;
[calc calculate];
}
/* Calculate Delegate */
-(void)didCalculate {
[[self delegate] didDoSomething];
}
Problem is that i from another place is calling the same thing it will call my latest delegate and this causes a lot of problems.
Question:
Is there a way to send the delegate as an object without having to set it as the property?
I’ve written it like this and Xcode give me warnings “Incomplete implementation of MyClass”
[myClass doSomethingWithDelegate:self];
And
-(void)doSomethingWithDelegate:(id)delegate {
CalculateStuff *calc = [[calculateStuff alloc] init];
[calc calculateWithDelegate:delegate];
}
/* Calculate Delegate */
-(void)didCalculateWithDelegate:(id)delegate {
[delegate didDoSomething];
}
EDIT
Just tried it out and it seems to work, but how can i get rid of the warnings in Xcode?
I think your problem is that you forgot to remove the declaration of
doSomethinganddidCalculatefrom your class’s interface (or class extension).Ideally you should create a protocol to ensure that your delegate has the required method(s). For instance:
Then use
id<DoSomethingDelegate>instead of justid.Passing a block would also be a valid solution to this problem, although it’s a bit trickier.