我试图为我的NSOperation添加一个选择器,但它给了我:
-[NSOperation sample]: unrecognized selector sent to instance 0x17e35830
2014-11-06 20:03:26.542 ****[1473:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSOperation sample]: unrecognized selector sent to instance 0x17e35830'
*** First throw call stack:
(0x303d8f53 0x3aa416af 0x303dc8e7 0x303db1d3 0x3032a598 0x820d5 0xa457d 0x590b9 0x3af24d7b 0x3af24d67 0x3af2b7c1 0x303a3821 0x303a20f5 0x3030cce7 0x3030cacb 0x34fda283 0x32baea41 0x78339 0x3af49ab7)
libc++abi.dylib: terminating with uncaught exception of type NSException
我的代码:
NSOperation *op = [[NSOperation alloc] init];
[op performSelector:@selector(sample)];
[MyQueue addOperation:op];
- (void)sample{
NSLog(@"sample");
}
答案 0 :(得分:0)
问题正是错误所说的,NSOperation
没有名为sample
的方法。您可以通过[self sample];
或[self performSelector:@selector(sample)];
答案 1 :(得分:0)
我刚刚使用NSInvocationOperation作为NSOperation的子类,我认为最好在指定对象上调用一个选择器。
苹果:
The NSInvocationOperation class is a concrete subclass of NSOperation that manages the
execution of a single encapsulated task specified as an invocation.You can use this
class to initiate an operation that consists of invoking a selector on
a specified object. This class implements a non-concurrent operation.
示例代码:
NSInvocationOperation *op = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(sample) object:nil];
答案 2 :(得分:0)
以这种方式尝试:
- (void)viewDidLoad
{
[super viewDidLoad];
NSOperationQueue *operationQueue=[NSOperationQueue new];
NSInvocationOperation *operation=[[NSInvocationOperation alloc]initWithTarget:self selector:@selector(changeText) object:nil];
[operationQueue addOperation:operation];
}
-(void)changeText{
........
..........
...........
}