iOS 8的当前用户位置 - 应用程序无法启动

时间:2016-01-11 17:18:00

标签: ios core-location currentlocation

我一直在使用一个简单的应用程序来使用Xcode中的Apple核心位置框架来获取用户当前位置,当我运行应用程序时出现错误。有人可以告诉我我做错了提前谢谢你。我在实现文件中有这段代码。

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController { CLLocationManager *locationManager;

}

- (void)viewDidLoad {
 [super viewDidLoad];
    // Do any additional setup after loading the view, typically   from   a nib.

    locationManager = [[CLLocationManager alloc] init];

}



- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (IBAction)getCurrentLocation:(id)sender {

locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;

[locationManager startUpdatingLocation];



}

#pragma mark - CLLocationManagerDelegate



- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSLog(@"didUpdateToLocation: %@", newLocation);
CLLocation *currentLocation = newLocation;

if (currentLocation != nil) {
    _longitudeLabel.text = [NSString stringWithFormat:@"%.8f",     currentLocation.coordinate.longitude];
    _latitudeLabel.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];
}
}





@end

这是标题文件......

#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>

@interface ViewController  : UIViewController <CLLocationManagerDelegate>


@property (strong, nonatomic) IBOutlet UILabel *latitudeLabel;

@property (strong, nonatomic) IBOutlet UILabel *longitudeLabel;

@property (strong, nonatomic) IBOutlet UILabel *addressLabel;

- (IBAction)getCurrentLocation:(id)sender;

@end

这是崩溃的输出......

2016-01-11 11:53:58.346 current location practice[1367:32081] ***     Terminating app due to uncaught exception 'NSUnknownKeyException', 
reason: '[<ViewController 0x7ff240d80cb0> setValue:forUndefinedKey:]:
 this class is not key value coding-compliant for the key getCurrentLocation.'
*** First throw call stack:
(
0   CoreFoundation                      0x000000010ee29f45   __exceptionPreprocess + 165
1   libobjc.A.dylib                     0x000000010e8a3deb objc_exception_throw + 48
2   CoreFoundation                      0x000000010ee29b89 -[NSException raise] + 9
3   Foundation                          0x000000010e470a6b -[NSObject(NSKeyValueCoding) setValue:forKey:] + 288
4   UIKit                               0x000000010f36104c -[UIViewController setValue:forKey:] + 88
5   UIKit                               0x000000010f58ea71 -[UIRuntimeOutletConnection connect] + 109
6   CoreFoundation                      0x000000010ed6aa80 -[NSArray makeObjectsPerformSelector:] + 224
7   UIKit                               0x000000010f58d454 -[UINib instantiateWithOwner:options:] + 1864
8   UIKit                               0x000000010f367c16 -[UIViewController _loadViewFromNibNamed:bundle:] + 381
9   UIKit                               0x000000010f368542 -[UIViewController loadView] + 178
10  UIKit                               0x000000010f3688a0 -[UIViewController loadViewIfRequired] + 138
11  UIKit                               0x000000010f369013 -[UIViewController view] + 27
12  UIKit                               0x000000010f24251c -[UIWindow addRootViewControllerViewIfPossible] + 61
13  UIKit                               0x000000010f242c05 -[UIWindow _setHidden:forced:] + 282
14  UIKit                               0x000000010f2544a5 -[UIWindow makeKeyAndVisible] + 42
15  UIKit                               0x000000010f1ce396 -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 4131
16  UIKit                               0x000000010f1d49c3 -[UIApplication _runWithMainScene:transitionContext:completion:] + 1750
17  UIKit                               0x000000010f1d1ba3 -[UIApplication workspaceDidEndTransaction:] + 188
18  FrontBoardServices                  0x000000011219f784 -[FBSSerialQueue _performNext] + 192
19  FrontBoardServices                  0x000000011219faf2 -[FBSSerialQueue _performNextFromRunLoopSource] + 45
20  CoreFoundation                      0x000000010ed56011 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
21  CoreFoundation                      0x000000010ed4bf3c __CFRunLoopDoSources0 + 556
22  CoreFoundation                      0x000000010ed4b3f3 __CFRunLoopRun + 867
23  CoreFoundation                      0x000000010ed4ae08 CFRunLoopRunSpecific + 488
24  UIKit                               0x000000010f1d14f5 -[UIApplication _run] + 402
25  UIKit                               0x000000010f1d630d UIApplicationMain + 171
26  current location practice           0x000000010e3224bf main + 111
27  libdyld.dylib                       0x0000000111d5892d start + 1
)
 libc++abi.dylib: terminating with uncaught exception of type     NSException
(lldb) 

有人可以告诉我我做错了什么以及我能做些什么来解决它?谢谢。

2 个答案:

答案 0 :(得分:0)

如下所示:

6   CoreFoundation                      0x000000010ed6aa80 -[NSArray makeObjectsPerformSelector:] + 224
7   UIKit                               0x000000010f58d454 -[UINib instantiateWithOwner:options:] + 1864

看来,您的IBAction不受getCurrentLocation方法的限制,或者被错误添加。

答案 1 :(得分:0)

它位于错误消息的顶部:

2016-01-11 11:53:58.346 current location practice[1367:32081] ***     Terminating app due to uncaught exception 'NSUnknownKeyException', 
reason: '[<ViewController 0x7ff240d80cb0> setValue:forUndefinedKey:]:
this class is not key value coding-compliant for the key getCurrentLocation.'

它告诉您,您正在尝试在类getCurrentLocation上使用名为ViewController的密钥,但该类没有该密钥。这很可能意味着你的故事板出了问题。导致这种情况的一种情况是将按钮连接到名为getCurrentLocation的操作,但随后删除该方法而不更新故事板。