Ios EXC\u性能选择器背景上的错误访问
我想在后台调用这个方法Ios EXC\u性能选择器背景上的错误访问,ios,objective-c,performselector,Ios,Objective C,Performselector,我想在后台调用这个方法 -(void)downloadImage_3:(NSString* )Path AtIndex:(int)i 我以这种方式呼叫,但它崩溃并显示EXC\u BAD\u ACCESS [self performSelectorInBackground:@selector(downloadImage_3:AtIndex:) withObject:[NSArray arrayWithObjects:@"http://www.google.com",i, nil]]; 如何在后
-(void)downloadImage_3:(NSString* )Path AtIndex:(int)i
我以这种方式呼叫,但它崩溃并显示EXC\u BAD\u ACCESS
[self performSelectorInBackground:@selector(downloadImage_3:AtIndex:) withObject:[NSArray arrayWithObjects:@"http://www.google.com",i, nil]];
如何在后台调用downloadImage_3:
方法
我哪里做错了 您不能在
@selector
中调用参数化函数。创建NSDictionary,然后在中使用对象传递该字典:
int atIndex = 2; // whatever index you want to pass
NSArray *arr = [NSArray arrayWithObjects:obj1,obj2,nil];
NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:atIndex],@"AtIndex",arr,@"ImagesUrl", nil];
[self performSelectorInBackground:@selector(downloadImage_3:) withObject:dictionary];
定义如下downloadImage_3
函数:
-(void)downloadImage_3:(NSDictionary *)dic {
// use dic and write your code
}
-(void)downloadImage_3:(NSDictionary *)dic
{
NSString *path = [dic valueForKey:@"Path"];
int i = [[dic valueForKey:@"Index"] intValue];
//Your code
}
试试这个
[self performSelectorInBackground:@selector(downloadImage_3:AtIndex:) withObject:[NSArray arrayWithObjects:@"http://www.google.com",i, nil] afterDelay:15.0];
或者试试这个
NSString* number = [NSString stringWithFormat:@"%d",i];
NSArray* arrayValues = [[NSArray alloc] initWithObjects:[[msg_array objectAtIndex:i] valueForKey:@"Merchant_SmallImage"],number, nil];
NSArray* arrayKeys = [[NSArray alloc] initWithObjects:@"Path",@"Index",nil];
NSDictionary* dic = [[NSDictionary alloc] initWithObjects:arrayValues forKeys:arrayKeys];
[self performSelectorInBackground:@selector(downloadImage_3:) withObject:dic];
定义downloadImage_3函数如下:
-(void)downloadImage_3:(NSDictionary *)dic {
// use dic and write your code
}
-(void)downloadImage_3:(NSDictionary *)dic
{
NSString *path = [dic valueForKey:@"Path"];
int i = [[dic valueForKey:@"Index"] intValue];
//Your code
}
对于接受多个参数的选择器,不能使用
performSelectorInBackground:withObject:
。其他答案的建议提供了一些工作,但它们都假设您可以操纵正在调用的方法,这并不总是可能的(为了清晰起见,甚至不是一个好主意)
我建议改为使用NSInvocation,因为它允许多个参数,或者使用GCD将块分派到后台队列(或除main以外的任何队列)
下面是NSInvocation的一个示例用法
NSMethodSignature *sig = [[self class] instanceMethodSignatureForSelector:@selector(downloadImage_3:AtIndex:)];
NSInvocation *inv = [NSInvocation invocationWithMethodSignature:sig];
[inv setTarget:self];
[inv setSelector:@selector(downloadImage_3:AtIndex:)];
[inv setArgument:(__bridge void *)@"http://www.google.com" atIndex:2];
NSUInteger i = 1;
[inv setArgument:&i atIndex:3];
[inv performSelectorInBackground:@selector(invoke) withObject:nil];
值得仔细检查,我在浏览器中编写了代码,因此我可能错过了编译器将获取的内容
作为补充说明,您应该重新考虑您的方法命名约定,一种更标准的方法命名方式是将方法命名为
-(void)downloadImage3:(NSString*)path atIndex:(int)i
。注意AtT索引如何以小写开头,以及没有下划线(在方法名的中间看起来很奇怪)。另外,值得注意的是,索引首选使用NSUInteger,因为NSArray通常使用NSUInteger(两者都可以,但有时int可能不够)。downloadImage\u 3:AtIndex:方法的内容是什么?这里的“i”是什么?不能将int对象添加到数组中。所以它一定是NSNumber。在这种情况下,您的方法参数也必须是NSNumber类型。i
是int
来自for loop
@Neha是否有其他方法可以在不使用NSArray
的情况下传递多个参数?值不是像您在中提供的1
和2
那样硬编码的带有objectsandkeys的字典
。如何提供动态值显示不兼容的整数在此处向id发送intNSDictionary*dictionary=[NSDictionary dictionary WithObjectsAndKeys:atIndex、@“atIndex”、arr、@“ImagesUrl”、nil]代码>snob-z:为字典传递的序列错误。dictionaryWithObjectsAndKeys将首先获取对象,然后获取该对象的键。此处显示EXC_BAD_访问权限NSDictionary*dictionary=[NSDictionaryWithObjectsAndKeys:[[msg_数组对象索引:i]valueForKey:@“Merchant_SmallImage”],@“Path”,i,“Index”,nil]
msg\u array是一个字典数组吗?实际上,由于无法向数组中添加int,所以会出现EXC\u BAD\u访问。因此,我已经将其转换为nsstring,然后我传递了参数,在-(void)downloadImage_3:(NSDictionary*)dic实现中,我将其转换回int。否则使用nsmutable dictionary。NSString*编号=[NSString stringWithFormat:@“%d”,i];NSMutableDictionary*dic=[[NSMutableDictionary alloc]init];[dic设置值:@“forKey:@“Path”];[dic设置值:编号forKey:@“索引”];[self-performSelectorInBackground:@selector(下载图像_3:),对象为dic]-(void)downloadImage_3:(NSMutableDictionary*)dic{NSString*path=[dic valueForKey:@“path”];int i=[dic valueForKey:@“index”];//您的代码}我应该从哪里调用downloadImage3
方法?NSInvocation为您执行此操作,在NSInvocation上调用-invoke将触发您指定的方法。在本例中,这就是您描述的下载方法。