当应用程序执行时,我正在获取用户位置,如果位置变量为@(null),我会阻止该 if 语句。在我的 FirstViewController.h 中:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[super viewDidLoad];
// Do any additional setup after loading the 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]];
}
}
在我的 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 are null");
TabBar *TB =[[TabBar alloc]init];
[TB performSelector:@selector(viewDidLoad)];
[TB performSelector:@selector(locationManager:didUpdateToLocation:fromLocation:)];
[self performSelector:@selector(GettingGlobalVariables)];
}
}
使用这个 if 语句,当变量为空时应用程序崩溃。有什么建议吗?