简体   繁体   English

IBAction方法中的应用崩溃

[英]App crash in IBAction method

Okay, so here is my code 好的,这是我的代码

-(IBAction)nextAction
{
    dispatch_async(myQueue, ^{ [self plusOneDate]; });
}

-(void)plusOneDate
{
    int hoursToAdd = 1;  

    // set up date components
    NSDateComponents *components = [[[NSDateComponents alloc] init] autorelease];
    [components setHour:hoursToAdd];

    // create a calendar
    NSCalendar *gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
    NSDate *newDate2 = [gregorian dateByAddingComponents:components toDate:appStateDate options:0];

    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"YYYYMMddHH0000"];
    dateString = [formatter stringFromDate:newDate2];
    [formatter release];

    appStateDate = newDate2;
    newDate2 = nil;
}

All it does is it adds 1 hour to a NSDate object (appStateDate), formats it, and sets the dateString variable. 它所做的只是向NSDate对象(appStateDate)添加1个小时,对其进行格式化并设置dateString变量。

Every time I press the UIButton in the app, connected to nextAction , the app crashes. 每次我在连接至nextAction的应用程序中按下UIButton时,应用程序都会崩溃。 I tried deleting the xib file and creating a new one. 我尝试删除xib文件并创建一个新文件。 It did not help, and still crashes. 它没有帮助,仍然崩溃。

In debug mode: 在调试模式下:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '(null): unrecognized selector sent to class 0x6242690'

Anyone who know why? 谁知道为什么? I'm getting frustrated. 我感到沮丧。

This isn't an answer so much as it is an observation. 这不是一个答案,而是一个观察。 The value assigned to appStateDate at the end of the method is an autoreleased object, and nowhere are you retaining it. 在方法末尾分配给appStateDate的值是一个自动释放的对象,您无处保留它。 You cannot rely on the "retain" attribute of the property (if appStateDate is in fact declared as a property somewhere), because you didn't use the property in the assignment. 您不能依赖属性的“保留”属性(如果appStateDate实际上在某处声明为属性),因为您没有在分配中使用该属性。

As soon as that IBAction finishes, the object pointed to by appStateDate is going to be reclaimed when the runloop drains the NSAutoreleasePool. IBAction完成后,当运行循环耗尽NSAutoreleasePool时,将回收appStateDate指向的对象。 Perhaps that's where the crash is coming from. 也许那是崩溃的来源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM