2012-08-14 33 views
-3

當傳遞給@selector的方法有參數時,需要語法方面的幫助。謝謝!Objective-C在performSelectorOnMainThread中傳遞帶參數的消息:

//This works 
dispatch_async(kBgQueue, ^{ 

[self performSelectorOnMainThread:@selector(hydrateList) withObject:nil waitUntilDone:YES]; 

}); 



//But what about when the method in the selector has arguments? 
//This is incorrect, syntactically 
    dispatch_async(kBgQueue, ^{ 

    [self performSelectorOnMainThread:@selector(hydrateListForCategory:self.categorySelected) withObject:nil waitUntilDone:YES]; 
    }); 
+3

搜索該方法和閱讀文檔。 [NSObject文檔](https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/nsobject_Class/Reference/Reference.html#//apple_ref/occ/instm/NSObject/performSelectorOnMainThread:withObject :waitUntilDone :) – MechEthan 2012-08-14 17:40:14

回答

1
[self performSelectorOnMainThread:@selector(hydrateListForCategory:) withObject:self.categorySelected waitUntilDone:YES]; 
3

提示:這就是爲什麼 「withObject:」 一部分是存在的。提示2:Apple爲他們的框架提供了相當不錯的文檔。

提示3:

[self performSelectorOnMainThread:@selector(hydrateListForCategory:) withObject:self.categorySelected waitUntilDone:YES];