如何在 NSInvocationOpreation 中传递对象



我创建了一个NSInvocationOpertion对象,如下所示

NSString *myString = @"Jai Hanuman";
NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(taskMethod) object:myString];
NSOperationQueue *operatioQueue = [[NSOperationQueue alloc] init];
    [operatioQueue addOperation:operation];

谁能告诉我如何在taskMethod中访问myString对象?可能吗?

- (void)taskMethod{
    NSLog(@"Oh! Invocation's working buck");
}

你可以试试这个:

将方法更改为:

- (void)taskMethod:(NSString *) message{
NSLog(@"Oh! Invocation's working buck");
        message=//your string object;
  }

NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(taskMethod:) object:myString];

使用一个参数定义方法:

- (void)taskMethod: (NSString *)theString {
    NSLog(@"Called with string: %@", theString);
}

并将操作创建为:

NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self 
         selector:@selector(taskMethod:) object:myString];

请注意选择器中的附加冒号,它表示该方法采取一个论点。

最新更新