2015-04-05 111 views
1

由於它在寫入文檔中,我的應用程序在啓動後立即崩潰。我真的不知道該怎麼做。謝謝你的幫助。這裏是代碼:我的應用程序在啓動後立即崩潰

#import "ViewController.h" 
@import CoreLocation; 

@interface ViewController() <CLLocationManagerDelegate> 
@property (strong, nonatomic) CLLocationManager *locationManager; 
@property (weak, nonatomic) IBOutlet UILabel *location; 
@property (weak, nonatomic) IBOutlet UILabel *date; 
@end 

@implementation ViewController 
-(void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    self.locationManager = [[CLLocationManager alloc] init]; 
    self.locationManager.delegate = self; 

    if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) { 
     [self.locationManager requestWhenInUseAuthorization]; 
    } 

    [self.locationManager startUpdatingLocation]; 

    //date 
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 

    [dateFormatter setDateFormat:@"MMMM d, YYYY"]; 

    NSString *dateString = [dateFormatter stringFromDate:[NSDate date]]; 
    NSLog(@"%@", dateString); 
    self.date.text = (@"%@", dateString); 
} 

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations 
{ 
    NSLog(@"%@", [locations lastObject]); 
    self.location.text = (@"%@", [locations lastObject]); 
} 

@end 

崩潰後,它顯示了這個: http://i.stack.imgur.com/k0eVO.png

+1

你能告訴我們什麼是在調試器打印? – Priyatham51 2015-04-05 10:19:42

+0

你看過http://stackoverflow.com/questions/24850128/ios-8-requestwheninuseauthorization-no-popup嗎? – luk2302 2015-04-05 11:26:43

+0

掛鉤lldb並檢查後面的跟蹤 – Wingzero 2015-04-05 13:20:41

回答

0

您的代碼是不完整的,你可以檢查出來,並再次

你已經得到

@selector(requestWhenInUseAuthorization) 
發佈

而你還沒有設置功能requestWhenInUseAuthorization

您在下面的代碼中放置了哪種方法。你已經把它放在viewDidLoad下面,但它不在任何方法內。檢查你的代碼正確

[self.locationManager startUpdatingLocation]; 

//date 
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 

[dateFormatter setDateFormat:@"MMMM d, YYYY"]; 

NSString *dateString = [dateFormatter stringFromDate:[NSDate date]]; 
NSLog(@"%@", dateString); 
self.date.text = (@"%@", dateString); 
} 
+0

我也很困惑,但它只是一個格式問題,代碼實際上是正確的,關於選擇器:他沒有定義它,他是在CLLocationManager上調用它,而不是自我 – luk2302 2015-04-05 11:25:08

+0

一切工作正常,但添加'self.date.text =(@「%@」,dateString);'線後它不再工作。我試圖刪除該行並再次運行,但沒有任何工作。 – pettyBright 2015-04-05 11:41:15

+0

請刪除此答案,因爲它不包含任何相關內容 – luk2302 2015-04-05 11:48:08

0

適用異常斷點在你的應用程序,然後再試一次。它會告訴你哪兒崩潰是