防止警告"PerformSelect可能导致泄漏,因为它的选择器未知"

Ray*_*yer 34 macos objective-c ios

可能重复:
performSelector可能导致泄漏,因为其选择器未知

我做了一个NSDictionary来将我的输入(NSString)转换为选择器."选择器地图"看起来如下:

[self setCmdSelectorMap:[NSDictionary dictionaryWithObjectsAndKeys: 
                         [NSValue valueWithPointer:@selector(doOpenBrowserByString:)], @"openBrowser",
                         [NSValue valueWithPointer:@selector(syncData:)], @"sync",
                         [NSValue valueWithPointer:@selector(getCachedString:)], @"getCachedString",
                         nil]];
Run Code Online (Sandbox Code Playgroud)

当我尝试获取其中一个选择器并按如下方式执行时,它会引发警告:

sel = [[_cmdMap objectForKey:command] pointerValue];
NSLog(@"selector determined : %@", NSStringFromSelector(sel));
[self performSelector:sel withObject:arguments];
Run Code Online (Sandbox Code Playgroud)

警告说:PerformSelector可能导致泄漏,因为它的选择器未知.有没有办法防止这种警告发生?或者是否有任何"更安全"的方式来执行此类操作?

多谢你们 :)

Ale*_*nov 128

只要用这个:

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
[self performSelector:sel withObject:arguments];
#pragma clang diagnostic pop
Run Code Online (Sandbox Code Playgroud)

  • 确实如此,@ Julian,但通过使用推/弹指令,您可以确保只在您查看过的地方忽略警告.我认为这是一种很好的做法. (12认同)
  • 我发现在类实现开始时使用`#pragma clang diagnostic ignored"-Warc-performSelector-leaks"就足够了. (7认同)
  • 虽然这是一种有效的删除警告的方法,但我不鼓励沉默警告.请参阅我在链接副本上的答案以获得推理. (4认同)

归档时间:

查看次数:

22192 次

最近记录:

13 年,5 月 前