【问题标题】:My app crashes immediately after startup我的应用程序在启动后立即崩溃
【发布时间】:2015-06-09 23:06:26
【问题描述】:

正如标题所写,我的应用程序在启动后立即崩溃。我真的不知道,该怎么办。谢谢你的帮助。这是代码:

#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

【问题讨论】:

  • 你能告诉我们调试器中打印的内容吗?
  • 连接 lldb 并检查回溯
  • 应用异常断点......并检查您是否将任何值存储到 userdefault 那么在启动应用程序和发展 userdefault 时它不应该为空
  • 你确定这个类是什么导致你的应用崩溃?!

标签: ios objective-c iphone xcode


【解决方案1】:

在您的应用中应用异常断点并重试。它会告诉您崩溃的确切位置

【讨论】:

    【解决方案2】:

    您的代码不完整,您可以检查一下并重新发布一次

    你有

    @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);
    }
    

    【讨论】:

    • 我也很困惑,但这只是一个格式问题,代码实际上是正确的,关于选择器:他没有定义它,他在 CLLocationManager 上调用它,而不是在自我
    • 一切正常,但添加self.date.text = (@"%@", dateString); 行后它不再工作了。我试图删除该行并再次运行,但没有任何效果。
    • 请删除此答案,因为它不包含任何相关内容
    猜你喜欢
    • 1970-01-01
    • 2013-12-13
    • 1970-01-01
    • 1970-01-01
    • 2020-01-25
    • 1970-01-01
    • 2013-12-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多