gpt4 book ai didi

ios - BOOL 方法在 NSLog 使用时返回 true 而它应该返回 false

转载 作者:行者123 更新时间:2023-11-29 12:57:18 25 4
gpt4 key购买 nike

这是一个位置应用程序。当我第一次在我的 iPhone 上运行该应用程序时,一个警告窗口询问我是否愿意允许位置服务。选择“确定”或“不允许”后,我可以点击设置为显示我当前位置的按钮。该按钮还设置为使用名为“locationServicesEnabled”的 BOOL 函数的值将 NSLog 打印到控制台,如果是则返回 yes/1,否则返回 no/0。

我的问题是,当我选择“不允许”选项并按下按钮时,NSLog 仍然将值 1 打印到控制台,这是不正确的。它应该是 0 或 false。连接到我的按钮的文本标签甚至显示“您的位置为空”,但出于某种原因,NSLog 始终显示 BOOL 值 1。

这是我的 ViewController.m 代码:

#import "ViewController.h" 
#import <CoreLocation/CoreLocation.h>

@interface ViewController () <CLLocationManagerDelegate>

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations;

@end

@implementation ViewController

- (void)viewDidLoad
{

[super viewDidLoad];

self.gpsLM = [[CLLocationManager alloc]init];

[self.gpsLM startUpdatingLocation];

}

- (void)didReceiveMemoryWarning

{
[super didReceiveMemoryWarning];
}


-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{


}

-(IBAction)gpsButton{

CLLocation * currentLocation = self.gpsLM.location;

self.gpsLabel.text = [NSString stringWithFormat:@"Your Location is %@", currentLocation];

NSLog(@"Location services enabled: %hhd",[CLLocationManager locationServicesEnabled]);

}

@end

最佳答案

您需要检查 [CLLocationManager authorizationStatus],而不是 locationServicesEnabled。授权状态返回应用特定的值,以下之一:

typedef enum {
kCLAuthorizationStatusNotDetermined = 0,
kCLAuthorizationStatusRestricted,
kCLAuthorizationStatusDenied,
kCLAuthorizationStatusAuthorized
} CLAuthorizationStatus;

希望名称是不言自明的。

关于ios - BOOL 方法在 NSLog 使用时返回 true 而它应该返回 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20747912/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com