ios - Returning a BOOL method inside a block? -
i have bool method returns yes or no inputted string. i'm able return yes
or no
, cannot seem able make network connection , return yes
or no
depending on server's response. tried using __block
, don't feel wait web request finish, there way return yes
or no
in success block without giving me error:
incompatible block pointer types sending 'bool(^)(nsurlsessiontask*__strong, nserror __strong' parameter of type 'void(^)(nsurlsessiontask...)
-(bool)customresponseforstring:(nsstring *)text { __block bool response_available; dispatch_async(dispatch_get_global_queue(dispatch_queue_priority_default, 0), ^{ afhttpsessionmanager *manager = [afhttpsessionmanager manager]; manager.responseserializer = [afhttpresponseserializer serializer]; [manager.responseserializer setacceptablecontenttypes:[nsset setwithobject:@"text/plain"]]; [manager get:[nsstring stringwithformat:@"http://example.com/response.php?input=%@", text] parameters:nil success:^(nsurlsessiondatatask *task, id responseobject) { nsdictionary *response = [nsjsonserialization jsonobjectwithdata:responseobject options:nsutf8stringencoding error:nil]; response_available = (bool)response[@"response_available"]; if (response_available) { [session sendtextsnippet:response[@"response"] temporary:no scrolltotop:no dialogphase:@"summary"]; } else { response_available = no; } [session sendtextsnippet:[[nsstring alloc] initwithdata:responseobject encoding:nsutf8stringencoding] temporary:no scrolltotop:no dialogphase:@"summary"]; [session sendrequestcompleted]; } failure:^(nsurlsessiondatatask *task, nserror *error) { //return no; }]; }); return response_available; }
your block definition syntax erroneous, because can return bool along other parameters in block.
- (void)fetchcurrentuserwithcompletion:(void (^)(bool success, user *user))completion;
this method called this:
[self.userprofilecontroller fetchcurrentuserwithcompletion:^(bool success, user *user) { if (success) { nslog(@"current user name: %@", user.fullname); } }];
if use afnetworking, check afhttprequestoperation
object handle completionblocks:
[requestoperation setcompletionblockwithsuccess:^(afhttprequestoperation *operation, id responseobject) { user *user = [self userfromresponseobject:responseobject]; if (completion) completion(yes, user); } failure:^(afhttprequestoperation *operation, nserror *error) { if (completion) completion(no, user); }];
Comments
Post a Comment