gpt4 book ai didi

ios - 如何解决 iOS 中的此警告?内存泄漏

转载 作者:行者123 更新时间:2023-11-29 03:07:22 24 4
gpt4 key购买 nike

函数中的这一行会产生一个警告:PerformSelector 可能会导致内存泄漏,因为它的选择器是未知的。我做错了什么?

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {

[_delegate1 performSelector:_selector1 withObject:json];

}

下面是方法performSelector

- (void)HttpRequest:(NSURL*)url PostString:(NSString *)poststring method:(int)method withselector:(SEL)selector withdelegate:(id)delegate
{

_responseData = [[NSMutableData alloc] init];
// procedures for parse at desired URL
request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData
timeoutInterval:5];

// set HTTP method
if (method == 0) {
[request setHTTPMethod:@"GET"];
// asks xml response
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; }

_selector1 = selector ;
_delegate1 = delegate ;

[self startConnection];

return;
}

最佳答案

你没有做错任何事情。编译器会发出警告,因为它还不知道选择器。如果有一个地方您收到此警告,请使用

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
[_delegate1 performSelector:_selector1 withObject:json];
#pragma clang diagnostic pop

如果有多个地方可以定义宏

#define SuppressPerformSelectorLeakWarning(Stuff) \
do { \
_Pragma("clang diagnostic push") \
_Pragma("clang diagnostic ignored \"-Warc-performSelector-leaks\"") \
Stuff; \
_Pragma("clang diagnostic pop") \
} while (0)

然后在所有引起警告的地方使用宏

SuppressPerformSelectorLeakWarning(

[_delegate1 performSelector:_selector1 withObject:json];
);

像这样调用每个选择器,它会抑制警告

关于ios - 如何解决 iOS 中的此警告?内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22616873/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com