iphone - performSelectorOnMainThread返回值

The*_*ter 9 iphone multithreading cocoa-touch

我有以下方法:

- (NSMutableArray *)getElements:(NSString *)theURL;
Run Code Online (Sandbox Code Playgroud)

我想知道是否有办法使用performSelectorOnMainThread调用该方法,以便我可以获得返回值.到目前为止,我尝试过:

myArray = [object performSelectorOnMainThread:@selector(getElements:)
                                   withObject:url waitUntilDone:YES];
Run Code Online (Sandbox Code Playgroud)

但它不起作用,因为performSelectorOnMainThread返回void.我该怎么解决这个问题?

Jac*_*kin 12

欢迎来到我的朋友多线程环境.

您需要将返回值存储在实例变量中,或通过withObject参数通过引用传入对象:

NSMutableDictionary *myDict;

[object performSelectorOnMainThread:@selector(getElements:)
                                   withObject:&myDict waitUntilDone:YES];
Run Code Online (Sandbox Code Playgroud)

您的方法原型现在应该如下所示:

- (void)getElements:(NSMutableDictionary **)objects;
Run Code Online (Sandbox Code Playgroud)

  • 这无法编译:"无法使用'NSMutableDictionary**'类型的右值初始化'id'类型的参数" (5认同)