Check if there is a delegate listening?

objective-c

Solution

even if the class that gets the delegate is already released and not exist anymore .

...have you tested that this is the case? If you aren't weak referencing your delegate then this won't be happening. If the delegate has actually been released then `self.delegate` should return `nil`, which would mean your if statement would evaluate to false.

The fact your if statement is evaluating to true suggests to me that your delegate is still set. Are you using ARC or manually managing memory? If it's the latter, you'll want your property to be set to `assign` - if it's the former, you'll want to set your delegate to `weak`.

Problem

I have a class that send delegate messages to another class. If the other class is not listening, the app is crash . I was looking for a way to check if the other class is listen before i send the message. so in the class that post the delegate , i have this condition the check that : ``` if ([self.delegate respondsToSelector:@selector(NewDataFromSocketWithString:WithCommand:)]) [self.delegate NewDataFromSocketWithString:final WithCommand:c]; ``` Where somehow , the condition is always true, even if the class that gets the delegate is already released and not exist anymore . How do i check the delegate before posting to eliminate crashes ?

Original source

Related problems