0

我创建了一个 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");
}
4

2 回答 2

1

你可以试试这个:

将您的方法更改为:

- (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];
于 2015-04-19T13:24:54.130 回答
1

用一个参数定义方法:

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

并将操作创建为:

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

注意选择器中的附加冒号,它表示该方法采用一个参数。

于 2015-04-19T13:25:47.863 回答