performSelector may cause a leak because its selector is unknown

[_editView setMyAction:^(NSString *actionString) {
           SEL sel = NSSelectorFromString(actionString);
           NSLog(@"%@",actionString);
           if([weakSelf respondsToSelector:sel])
           {
               [weakSelf performSelector:sel];
           }
       }];

出现警告:performSelector may cause a leak because its selector is unknown。
我们在调用这些方法的时候有可能返回的是void 或者其他 non-Objects,我们可以忽略这个警告,但是不建议这么做。我们知道oc的内存管理机制,有retain必然有release。在arc模式下,这些都由编译器帮我们做了。但是,假如我们的方法返回的是non-Objects(当然,包括void),这时retain或者release,我们的程序就有可能crash掉。

解决办法:

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
    [target performSelector: sel withObject: nil];
#pragma clang diagnostic pop
 [_editView setMyAction:^(NSString *actionString) {
            SEL selector = NSSelectorFromString(actionString);
            IMP imp = [weakSelf methodForSelector:selector];
            void (*func)(id, SEL) = (void *)imp;
            NSLog(@"%@",actionString);
            if([weakSelf respondsToSelector:selector])
            {
                func(weakSelf, selector);
            }
        }];

参考:https://stackoverflow.com/questions/7017281/performselector-may-cause-a-leak-because-its-selector-is-unknown

你可能感兴趣的:(performSelector may cause a leak because its selector is unknown)