ios - Objective C variable being set to null -
so having problem variable in code becoming null after being set non null value. variable set here:
mklocalsearchcompletionhandler completionhandler = ^(mklocalsearchresponse *response, nserror *error) { self.places = [response mapitems]; }; when call self.places outsides code snippet, self.places null. suggestions?
here's class (i took out methods brevity):
#import "addfencecontroller.h" #import "placeannotation.h" @interface addfencecontroller () <uisearchbardelegate, uisearchcontrollerdelegate, cllocationmanagerdelegate, mkmapviewdelegate> @property (nonatomic, strong) cllocationmanager *locationmanager; @property (nonatomic, strong) nsmutablearray *locations; @property (nonatomic, strong) uisearchcontroller *searchcontroller; @property (nonatomic, strong) mklocalsearchrequest *localsearchrequest; @property (nonatomic, strong) mklocalsearch *localsearch; @property cllocationcoordinate2d coords; @property (nonatomic, strong) placeannotation *annotation; @property (nonatomic, strong) nsarray *places; @end @implementation addfencecontroller { } @synthesize latitude; @synthesize longitude; @synthesize region; @synthesize radius; @synthesize geofence; @synthesize places; - (void)viewdidload { [self.ibsearchbar setdelegate:self]; self.latitude = [self.geofence latitude]; self.longitude = [self.geofence longitude]; self.radius = [self.geofence radius]; } - (void)startsearch:(nsstring *)searchstring { if (self.localsearch.searching) { [self.localsearch cancel]; } mklocalsearchrequest *request = [[mklocalsearchrequest alloc] init]; request.naturallanguagequery = searchstring; mklocalsearchcompletionhandler completionhandler = ^(mklocalsearchresponse *response, nserror *error) { if (error != nil) { nsstring *errorstr = [[error userinfo] valueforkey:nslocalizeddescriptionkey]; uialertview *alert = [[uialertview alloc] initwithtitle:@"could not find places" message:errorstr delegate:nil cancelbuttontitle:@"ok" otherbuttontitles:nil]; [alert show]; } else { self.places = [response mapitems]; } [uiapplication sharedapplication].networkactivityindicatorvisible = no; }; if (self.localsearch != nil) { self.localsearch = nil; } self.localsearch = [[mklocalsearch alloc] initwithrequest:request]; [self.localsearch startwithcompletionhandler:completionhandler]; [uiapplication sharedapplication].networkactivityindicatorvisible = yes; self.region = [self setupgeofence:self.geofence.latitude.doublevalue:self.geofence.longitude.doublevalue]; } - (void)searchbarsearchbuttonclicked:(uisearchbar *)searchbar { [searchbar resignfirstresponder]; [self startsearch:self.ibsearchbar.text]; mkmapitem *mapitem = [self.places objectatindex:0]; self.latitude = [nsnumber numberwithdouble:mapitem.placemark.coordinate.latitude]; self.longitude = [nsnumber numberwithdouble:mapitem.placemark.coordinate.longitude]; [self.ibmapview setusertrackingmode:mkusertrackingmodenone]; .... } .... @end
there problem flow of program: when call
[self startsearch:self.ibsearchbar.text]; you kick off search, , return right away. access self.places in next statement, i.e. here
mkmapitem *mapitem = [self.places objectatindex:0]; because search did not have opportunity return!
a proper way handle add method class handles search completion, , call after setting self.places:
// make change in asynchronous handler: if (error != nil) { .... [alert show]; } else { self.places = [response mapitems]; // add line [self searchbarsearchcompleted]; } .... // add method class - (void)searchbarsearchcompleted { mkmapitem *mapitem = [self.places objectatindex:0]; self.latitude = [nsnumber numberwithdouble:mapitem.placemark.coordinate.latitude]; self.longitude = [nsnumber numberwithdouble:mapitem.placemark.coordinate.longitude]; [self.ibmapview setusertrackingmode:mkusertrackingmodenone]; .... } this splits searchbarsearchbuttonclicked: method "before" , "after" parts. "before" part remains same - tells startsearch: initiate search.
the "after" part gets moved separate method. takes on when search complete. if not need places outside of searchbarsearchcompleted, may eliminate variable altogether, , pass array parameter.
Comments
Post a Comment