gpt4 book ai didi

ios - performSelector 可能会导致泄漏,因为它的选择器是未知的 IN Singleton Class/FUNCTION Pointer -Passing Function as parameter

转载 作者:技术小花猫 更新时间:2023-10-29 10:55:35 24 4
gpt4 key购买 nike

@interface URLClass : NSObject
{
id target;
SEL funObj;
}
+ (URLClass *)sharedInstance;
-(void)theBigFunction:(SEL)func :(id)target;
@property (nonatomic,retain) SEL funObj;

#import "URLClass.h"

static URLClass *instance = NULL;


@implementation URLClass
{
NSMutableData *webData;
}
- (id)init
{
if ( self = [super init] )
{

}
return self;
}

+ (URLClass *)sharedInstance
{
@synchronized([URLClass class])
{
if (!instance)
instance = [[super alloc] init];
return instance;
}
return nil;
}
-(void)theBigFunction:(SEL)func :(id)targetObj{

funObj =func;
target=targetObj;
NSURL *URL = [NSURL URLWithString:@"urlString"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];

if( connection )
{
webData = [NSMutableData data] ;
}
else
{
NSLog(@"theConnection is NULL");
}
}



-(BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace
{

return YES;
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[webData setLength: 0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[webData appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"ERROR with theConenction");
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{

NSError *error;
id jsonObj = [NSJSONSerialization JSONObjectWithData:webData options:0 error:&error];
if (jsonObj!=nil && error==nil) {
if ([jsonObj isKindOfClass:[NSDictionary class]]) {
NSDictionary *dic=(NSDictionary*)jsonObj;
NSLog(@"DIC jsonObj %@ ",dic);
NSArray *array=[dic objectForKey:@"items"];
NSLog(@"array jsonObj %@ %d",array,[array count]);
}else if ([jsonObj isKindOfClass:[NSArray class]]) {

NSArray *arr=(NSArray*)jsonObj;
NSLog(@"arr jsonObj %@ ",arr);
}
}
[target performSelector:funObj];
// Not gEtting called the aboue line

//performSelector may cause a leak because its selector is unknown 警告在上一行

当我计划执行以下行时,来自任何类的表单代码。它没有被调用。

-(void)loginPress:(id)sender{
URLClass *rlOBJ=[URLClass sharedInstance];
[rlOBJ theBigFunction:@selector(upd_Handler:) :self];
}
- (void) upd_Handler: (id) value{

NSLog( @"Seccess");
}

最佳答案

现代方法是让您的类接受完成 block 而不是目标/选择器。然后您就不必插入一堆丑陋的编译器抑制行,并且您会获得更大的灵 active 。

关于ios - performSelector 可能会导致泄漏,因为它的选择器是未知的 IN Singleton Class/FUNCTION Pointer -Passing Function as parameter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21433873/

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