완료 블록을 사용하여 메서드를 NSURLSessionTask로 호출하는 방법을 이해하는 데 몇 가지 문제가 있습니다. 어떻게하면 오류없이 getForecastAndConditionsForZipCode 메서드를 제대로 호출하는지 궁금합니다. 고맙습니다!Objective-C 완료 블록
Api.h :
typedef void (^WeatherAPICompletionBlock)(BOOL success, NSDictionary *result, NSError *error);
- (NSURLSessionDataTask *)getForecastAndConditionsForZipCode:(NSString *)zipCode withCompletionBlock:(WeatherAPICompletionBlock)completionBlock;
Api.m
- (NSURLSessionDataTask *)getForecastAndConditionsForZipCode:(NSString *)zipCode withCompletionBlock:(WeatherAPICompletionBlock)completionBlock
{
if (!self.APIKey) {
NSAssert(NO, @"API Key not set", nil);
completionBlock(NO, nil, [self missingAPIKeyError]);
return nil;
}
if (![NSThread isMainThread]) {
NSAssert(NO, @"API client method must be called on the main thread", nil);
completionBlock(NO, nil, [self genericError]);
return nil;
}
// Create the path
NSString *pathString = [NSString stringWithFormat:@"/api/%@%@/q/%@.json", self.APIKey, kWeatherAPIConditionsPath, zipCode];
// To avoid a retain cycle
__weak __typeof(self)weakSelf = self;
// Start the request
return [self GET:pathString parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
if (!responseObject || ![responseObject isKindOfClass:[NSDictionary class]] || [responseObject count] == 0) {
DLog(@"Invalid responseObject: %@", responseObject);
completionBlock(NO, nil, [weakSelf genericError]);
return;
}
completionBlock(YES, responseObject, nil);
} failure:^(NSURLSessionDataTask *task, NSError *error) {
DLog(@"Error with getForcastForLocation response: %@", error);
completionBlock(NO, nil, error);
}];
}
ViewController.m (여기 내가 getForecastAndConditionsForZipCode 방법
를 호출하는 방법을 이해하지 못하는 곳이다import "Api.h"
- (IBAction)runApi:(UIButton *)sender {
WeatherAPIClient *weatherApiClient = [[WeatherAPIClient alloc] init];
NSURLSessionDataTask *test = [weatherApiClient getForecastAndConditionsForZipCode:@"55345" withCompletionBlock:^(YES, result, error)];
}
진지하게 질문 제목을 변경 할 수 있습니까? – gnasher729
@ gnasher729, 그의 제목에 "반대"가 있습니까? 나는 당신의 이의 제기에 실패합니다. :) –