performSelector may cause a leak because its selector is unknown

后端 未结 19 2248
小蘑菇
小蘑菇 2020-11-22 01:54

I\'m getting the following warning by the ARC compiler:

\"performSelector may cause a leak because its selector is unknown\".

Here\'s what

19条回答
  •  自闭症患者
    2020-11-22 02:40

    Instead of using the block approach, which gave me some problems:

        IMP imp = [_controller methodForSelector:selector];
        void (*func)(id, SEL) = (void *)imp;
    

    I will use NSInvocation, like this:

        -(void) sendSelectorToDelegate:(SEL) selector withSender:(UIButton *)button 
    
        if ([delegate respondsToSelector:selector])
        {
        NSMethodSignature * methodSignature = [[delegate class]
                                        instanceMethodSignatureForSelector:selector];
        NSInvocation * delegateInvocation = [NSInvocation
                                       invocationWithMethodSignature:methodSignature];
    
    
        [delegateInvocation setSelector:selector];
        [delegateInvocation setTarget:delegate];
    
        // remember the first two parameter are cmd and self
        [delegateInvocation setArgument:&button atIndex:2];
        [delegateInvocation invoke];
        }
    

提交回复
热议问题