传递int作为performSelectorOnMainThread的对象:

Win*_*ton 1 primitive cocoa-touch objective-c

我需要调用:

[self performSelectorOnMainThread:@selector(chooseURL:) withObject:myIndex waitUntilDone:YES];
Run Code Online (Sandbox Code Playgroud)

但我的问题是,这myIndex是一个int,而不是一个对象.有解决方法吗?

Ste*_*ser 5

您必须将int包装在对象中,因为int是基本类型而不是对象.例如:

[self performSelectorOnMainThread:@selector(chooseURL:) 
                       withObject:[NSNumber numberWithInt:myIndex] 
                    waitUntilDone:YES];
Run Code Online (Sandbox Code Playgroud)

当然,您必须编辑您的chooseURL:方法以接受NSNumber而不是int.打开包装:

int myInt = [myIndex intValue];
Run Code Online (Sandbox Code Playgroud)