2

I am trying to get rid of a PerformSelector warning, according to the solution described in this answer: performSelector may cause a leak because its selector is unknown

The line that is causing an issue is:

if ([self.target respondsToSelector:self.action]) {
    [self.target performSelector:self.action withObject:self];
}

I have tried to solve it by changing the code to:

if ([self.target respondsToSelector:self.action]) {
    SEL selector = self.action;
    IMP imp = [self.target methodForSelector:selector];
    void (*func)(id, SEL) = (void *)imp;
    func(self.target, selector);
}

Although it no longer reports an issue, the app crashes when this method is invoked. How can I solve this issue? This question is not a duplicate because I have read the other answer and still am unable to solve the problem.

Community
  • 1
  • 1
Youngin
  • 261
  • 4
  • 14
  • 1
    You are probably missing the parameter to be passed to "func". Try adding third parameter to func variable while declaring and calling. – mithlesh jha Dec 23 '14 at 19:28
  • 1
    Thanks for the response. You were correct I was missing the self object. Here's what worked for me: if ([self.target respondsToSelector:self.action]) { NSObject *selfOBJ = self; SEL selector = self.action; IMP imp = [self.target methodForSelector:selector]; void (*func)(id, SEL, id) = (void *)imp; func(self.target, selector, selfOBJ); } – Youngin Dec 23 '14 at 19:38

0 Answers0