1

I have a custom BackBarButton where I have a property to store a selector which can change in some cases. So I can't use delegation really smoothly.

Warning description

What can I do to get rid of this warning without changing the 'workflow' to delegation? The property is defined by using this:

@property (nonatomic, strong) id<SPUniversalBackBarButtonItemDelegate> delegate;
@property (nonatomic, assign) SEL delegationSelector;

I also tried to use this code, but it says 'No known instance method for selector...' and 'Implicit conversation of an Objective-C pointer to IMP'...

IMP imp = [[self delegate] methodForSelector:[self delegationSelector]];
void (*func)(id, SEL) = (void *)imp;
func([self delegate], [self delegationSelector]);
propstm
  • 3,461
  • 5
  • 28
  • 41
regetskcob
  • 1,172
  • 1
  • 13
  • 35

2 Answers2

6

You can expose your method in the protocol declaration. Then you will be able to call it without the need of a selector. And you won't have that warning.

OR

if you just want to get rid of the warning:

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
        //code here will ignore the warning
#pragma clang diagnostic pop
wolffan
  • 1,084
  • 10
  • 15
  • just FYI silencing a warning such as a a potential leak from an unknown selector is ill-advised, as the warning message is there for a reason. – Will Von Ullrich Oct 17 '17 at 21:23
4

First why is your delegate strong? I really doubt you want a strong delegate. Most of the times you want a weak delegate because you don't want your object to dictate the memory status of your delegate.

An alternative to perform selector is NSInvocation:

NSMethodSignature * mySignature = [self.delegate methodSignatureForSelector:self.delegationSelector];
NSInvocation * myInvocation = [NSInvocation
invocationWithMethodSignature:mySignature];
myInvocation.target = self.delegate;
myInvocation.selector = self.delegationSelector;
[myInvocation invoke];
Community
  • 1
  • 1
Tiago Almeida
  • 14,081
  • 3
  • 67
  • 82
  • Thanks :) Changed the delegate to weak and the NSInvocation implementation is working really fine :) – regetskcob Feb 02 '15 at 11:17
  • It's crashing because [NSMutableArray instanceMethodSignatureForSelector:self.delegationSelector]; returns nil. Why NSMutableArray? – regetskcob Feb 02 '15 at 13:43
  • Probably returns nil because of this : http://stackoverflow.com/questions/9301450/is-it-possible-to-invoke-a-msg-from-a-different-class . So, you essentially need to do `NSMethodSignature * mySignature = [self.delegate methodSignatureForSelector:self.delegationSelector];` – Tiago Almeida Feb 02 '15 at 14:14