ios - how can i avoid crash due to preventing variables being @(null)? -
i gettin users location when application executed , if location variables @(null) prevent whit if statement. @ firstviewcontroller.h:
- (void)viewdidload { [super viewdidload]; // additional setup after loading view. [super viewdidload]; // additional setup after loading view. locationmanager = [[cllocationmanager alloc] init]; locationmanager.delegate = self; locationmanager.desiredaccuracy = kcllocationaccuracyhundredmeters; [locationmanager startupdatinglocation]; } cllocationmanager *locationmanager; - (void)locationmanager:(cllocationmanager *)manager didupdatetolocation:(cllocation *)newlocation fromlocation:(cllocation *)oldlocation { cllocation *currentlocation = newlocation; if (currentlocation != null) { [(appdelegate *)[[uiapplication sharedapplication] delegate] setlatitude:[nsstring stringwithformat:@"%.8f", currentlocation.coordinate.latitude]]; [(appdelegate *)[[uiapplication sharedapplication] delegate] setlongitude:[nsstring stringwithformat:@"%.8f", currentlocation.coordinate.longitude]]; } } at secondviewcontroller:
-(void) gettingglobalvariables{ nsstring *userscurrentlatitude = [(appdelegate *)[[uiapplication sharedapplication] delegate] latitude]; nsstring *userscurrentlongitude = [(appdelegate *)[[uiapplication sharedapplication] delegate] longitude]; nsnumberformatter * singsingsing = [[nsnumberformatter alloc] init]; [singsingsing setnumberstyle:nsnumberformatterdecimalstyle]; nsnumber * mynumberlatitude = [singsingsing numberfromstring:userscurrentlatitude]; nsnumberformatter * shecametomeonemorning = [[nsnumberformatter alloc] init]; [shecametomeonemorning setnumberstyle:nsnumberformatterdecimalstyle]; nsnumber * mynumberlongitude = [shecametomeonemorning numberfromstring:userscurrentlongitude]; nslog(@"mynumberlatitude:%@,mynumberlongitude:%@",mynumberlongitude,mynumberlatitude); if (!mynumberlatitude) { nslog(@"location variables null"); tabbar *tb =[[tabbar alloc]init]; [tb performselector:@selector(viewdidload)]; [tb performselector:@selector(locationmanager:didupdatetolocation:fromlocation:)]; [self performselector:@selector(gettingglobalvariables)]; } } with if statement, when variables null application crush. suggestion?
use this,
if (currentlocation != [nsnull null] | currentlocation !=nil) { //write code here. }
Comments
Post a Comment