-1

I have been working on a simple app to get the users current location using the Core Location framework from Apple in Xcode and when i run the app i get an error. Can someone please tell me what i did wrong thank you in advance. I have this code here in the Implementation file.

#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

This is the Header file...

#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

And this is the output of the crash...

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) 

Can someone please tell me what i did wrong and what i can do to fix it? Thank you.

  • most likely you have configured something wrong in Interface builder. Check that your xib/storyboard points to the correct class and then check all your IBOutlets and IBActions – Erik Johansson Jan 11 '16 at 17:25

2 Answers2

0

As per below errors:

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

It seems, your IBAction is not bounded with getCurrentLocation method, or is added wrongly.

Ankit Thakur
  • 4,739
  • 1
  • 19
  • 35
  • checkout this link: http://stackoverflow.com/questions/3088059/what-does-this-mean-nsunknownkeyexception-reason-this-class-is-not-key?rq=1 – Ankit Thakur Jan 11 '16 at 17:37
0

It's right at the top of the error message:

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.'

It's telling you that you're trying to use a key called getCurrentLocation on your class ViewController, but the class doesn't have that key. Most likely this means something's wrong with your storyboard. One scenario that would cause this would be connecting a button to an action named getCurrentLocation, but then deleting that method and not updating the storyboard.

Tom Harrington
  • 69,312
  • 10
  • 146
  • 170