簡體   English   中英

使用Objective c在后台調用類方法

[英]Call Class Method in background using Objective c

在下面的摘錄中,

/*A ClassName with instanceMethod and ClassMethod  */

-(void)instanceMethod;

+(void)ClassMethod;

/*To call a instance method in background */

ClassName  class1obj = [ClassName alloc] init];

[class1obj performSelectorInBackground:@selector(instanceMethod) withObject:nil];

同樣,如何使用performSelectorInBackground在后台調用ClassMethod?

如果可能,請解釋! 請伙計們攜手..

打電話吧

[ClassName performSelectorInBackground:@selector(ClassMethod) withObject:nil];

因為類本身就是對象,所以這將起作用。

請嘗試self而不是班級名稱

 [self performSelectorInBackground:@selector(methodTobeCalled) withObject:nil];

希望這對你有用

你應該看看GCD(Grand Central Dispatch),它解決了一般問題“如何在后台執行代碼”。 無論是調用類方法,調用實例方法,擲骰子,寫入文件等等都無關緊要。

例:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    NSLog (@"This is code running in the background");
    [MyClass someClassMethod];
    [myInstance someMethodWithInt:1 bool:YES string:@"some string"];
    NSLog (@"Finished with the background code");
});

使用任意代碼; 不需要使用選擇器,不需要編寫方法只是為了讓選擇器在后台運行,不需要將參數轉換為NSObject(你不能將performSelector與int或BOOL參數一起使用)。 Xcode會自動為您填寫大部分內容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM