开发者

problem in performSelectorOnMainThread in iphone

i did this


[(OfficeLinQViewController*)sharedManager.m_o  performSelectorOnMainThread:@selector(findLocalListing::)
                                                               withObject:(folderList,path) 
                                                            waitUntilDone:NO];
but the problem is that开发者_运维问答 in findLocalListing function in both the arguments path is saved not folderList.


Look again at the withObject: part. It says withObject, not withObjects. You can only pass one argument to the selector.

I usually solve these problems with a wrapper method like this.

[(OfficeLinQViewController*)sharedManager.m_o performSelectorOnMainThread:@selector(findLocalListingWithArgumentArray:)withObject:[NSArray arrayWithObjects:folderList,path, nil] waitUntilDone:NO];

- (void)findLocalListingWithArgumentArray:(NSArray *)argArray {
    [self findLocalListing:[argArray objectAtIndex:0] inPath:[argArray objectAtIndex:1]];
}

Oh, and you should rename findLocalListing:: to something useful.


I agree with every other answer - you can only pass one object. However, I usually solve it a different way.

I use an NSDictionary to hold your objects

NSDictionary *info = [NSDictionary dictionaryWithObjectsAndKeys:
                      folderList, @"folderList", path, @"path", nil];
[(OfficeLinQViewController*)sharedManager.m_o performSelectorOnMainThread:@selector(findLocalListing:) withObject:info waitUntilDone:NO];

And in findLocalListing

- (void)findLocalListing:(NSDictionary *)info {
    NSString *path = [info objectForKey:@"path"];
    NSArray *folderList = [info objectForKey:@"folderList"];

This lets you pass as many objects as you want :)


If you don't like an NSDictionary, you could create your own object and pass that instead :)


You can only pass one object to performSelector. I note that you try to pass in two using the form:

(folderList,path)

This tuple form is allowed in C, but it doesn't do what you think it does. I believe it evalutes each item in the tuple, but overall the tuple evaluates to the valuation of the last item.

If you need to get a few things through to the selector in question, you have a few options:

  • wrap them in a container object (either roll your own, or use a collection of somesort, which admittedly feels a bit loose)
  • have the selector code able to obtain the values from somewhere without being passed in as params (e.g. accessible as a property from somewhere)

Btw, putting things like . and _ in variable names is extremely non-standard. How much you care about that is down to you, but if other people have to read your code (including people on this site), you might consider sticking to normal naming conventions.


I think it should be more like this:

[(OfficeLinQViewController*)sharedManager .m_o performSelectorOnMainThread:@selector(findLocalListing:) withObject:(folderList) waitUntilDone:NO];

Note that I removed the second colon from the @selector parameter as well as the second argument in the withObject: parameter. performSelectorOnMainThread does not support sending multiple objects without first wrapping them in a collection of some sort.

You could, however, add a Category to NSObject as mentioned here. It should be fine but I'm always wary of adding methods to base objects.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜