2016-01-11 99 views
-1

我一直在研究一個簡單的應用程序,以便在Xcode中使用Apple的Core Location框架獲取用戶當前位置,並且當我運行應用程序時出現錯誤。有人可以告訴我我做錯了什麼,提前謝謝你。我在這個實現文件中有這個代碼。iOS 8的當前用戶位置 - 應用程序不會啓動

#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) 

有人能告訴我我做錯了什麼,並我能做些什麼來解決它?謝謝。

+0

很可能你在Interface Builder中配置了錯誤的東西。檢查你的xib/storyboard指向正確的類,然後檢查你所有的IBOutlets和IBActions –

回答

0

按如下錯誤:

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

看來,你的IBAction爲不與getCurrentLocation法爲界,或者錯誤地添加。

+0

結帳這個鏈接:http://stackoverflow.com/questions/3088059/what-does-this-mean-nsunknownkeyexception-reason-this-類是 - 不鍵?RQ = 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.' 

它告訴你,你要對你的ViewController類使用一個名爲getCurrentLocation關鍵,但類沒有這把鑰匙。這很可能意味着你的故事板有問題。導致這種情況的一種情況是將按鈕連接到名爲getCurrentLocation的操作,但是然後刪除該方法而不更新故事板。

相關問題