我在NSObject类中编写了一个带有完成块(completion block)的方法,并从UIViewController中调用此方法,它完美运行。但是我如何在此方法中传递一个NSString参数?下面是我的代码片段:
-(void)testingFunction:(void(^)(NSMutableArray* result))handler{
NSMutableArray *dataArray = [[NSMutableArray alloc] init];
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlstring]];
NSString *authStr = @"";
NSData *authData = [authStr dataUsingEncoding:NSUTF8StringEncoding];
NSString *authValue = [NSString stringWithFormat: @"Basic %@",[authData base64EncodedStringWithOptions:0]];
[request setValue:authValue forHTTPHeaderField:@"Authorization"];
//create the task
NSURLSessionDataTask* task = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
[dataArray addObject:[[json objectForKey:@"query"] objectForKey:@"geosearch"]];
dispatch_async(dispatch_get_main_queue(), ^{
handler(dataArray) ;
});
}];
[task resume];
}
我需要从我的UIViewController中调用此方法。
[[AllFunction sharedInstance] testingFunction:^(NSMutableArray* testResult){
[somearray addObject:testResult];
NSLog(@"Result was %@", somearray);
[self.tableView reloadData];
}];
NSString*
传递给testingFunction
还是传递给 block? - Moumou