Ответ от вызова API возвращается асинхронно, поэтому вам нужно написать блок для возврата результата, пока вы получили ответ от API.
Приведенный ниже код поможет вам выполнить ваши требования.
Этот код будет возвращать ответ асинхронно
-(void) searcherMethod: (NSString *) text responseCompletion:(void (^_Nullable)(NSDictionary *))responseCompletion{
[ReadService readFromApi:APIURL
whenFinish:^(id responseObject) {
//HERE I have A response from API
NSDictionary *response = (NSDictionary*)responseObject;
//HOW searcherMethod can return the response dict?
if (responseCompletion) {
responseCompletion(response);
}
}];
}
Этот код будет возвращать ответ синхронно
-(NSDictionary *) searcherMethod: (NSString *) text {
NSDictionary *response = nil;
// create the semaphore for synchronously call
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
[ReadService readFromApi:APIURL
whenFinish:^(id responseObject) {
//HERE I have A response from API
response = (NSDictionary*)responseObject;
// fire signal for semaphore so your code will execute further
dispatch_semaphore_signal(semaphore);
}];
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
return response;
}
Семафорблокируя выполнение до тех пор, пока сигнал не будет получен, ваш метод вернется после получения ответа.
Вы можете использовать приведенный ниже код для вызова метода.
[self searcherMethod:@"responseText" responseCompletion:^(NSDictionary *responseDict) {
//result
NSLog(@"Response : %@",responseDict);
}];
Теперь вы можете вызывать свой метод синхронно
NSDictinary *response = [self searcherMethod:text]
Надеюсь, это поможет вам.