繁体   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