如何将整数发送到id类型的指针

时间:2011-07-28 08:19:24

标签: ios objective-c xcode multithreading integer

对于某些人来说,这可能是一个简单的问题。对我来说很头疼。如何在此方法中将整数作为id对象发送:

    [self performSelectorOnMainThread:@selector(requestImageAtIndex:) withObject:index waitUntilDone:NO];

索引是这里的整数。

提前谢谢大家。

P.S。 “没有愚蠢的问题,只有愚蠢的答案。” Marshall Loeb

3 个答案:

答案 0 :(得分:1)

为什么不将它包装在NSNumber对象中?

[self performSelectorOnMainThread:@selector(requestImageAtIndex:)
                       withObject:[NSNumber numberWithInteger:index]
                    waitUntilDone:NO];

答案 1 :(得分:0)

要在这种情况下传递普通值,您需要将它们包装到某些objective-c对象中。在您的情况下,NSNumber将起作用(当然requestImageAtIndex方法必须将NSNumber对象作为参数才能使其工作):

[self performSelectorOnMainThread:@selector(requestImageAtIndex:)
                       withObject:[NSNumber numberWithInt:index]
                    waitUntilDone:NO];

答案 2 :(得分:0)

int index;

index=5;



[self performSelectorOnMainThread:@selector(requestImageAtIndex:) withObject:[NSNumber numberWithInt:index] waitUntilDone:NO];


-(void)requestImageAtIndex:(NSNumber*)number{

    int i=[number intValue];

}