简体   繁体   中英

NSDictionary passed to NSOperation is null inside the invoked method

I'm using the code below to pass multiple values to NSOperation.

I have defined an NSDictionary but I can't access on it (it returns always null) within the method called.

Below is my code to invoke the method

NSDictionary *params = [[NSDictionary alloc] initWithObjectsAndKeys:
@"key1", @"value1",
@"key2", @"value2",
nil];

NSOperationQueue *queue = [NSOperationQueue new];
NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(method:) object:params];
[queue addOperation:operation];

Here is my method definition

- (void) method: (NSDictionary *) params {
     NSLog(@"value1: %@", [[params objectForKey:@"key1"] stringValue]);
}

The method called method is called but the NSLog prints always null when I actually aspect to see value1 as per my dictionary definition.

Am I missing something?

Tnx

As the method name initWithObjectsAndKeys indicates, in your case, @"key1" is the value/object and @"value1" is the key. Also, there is no stringValue method in the NSString object. So, try:

NSLog(@"value1: %@", [params objectForKey:@"value1"]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM