0

我已经像这样使用 timmer 调用了一个方法。

 timmer=    [NSTimer scheduledTimerWithTimeInterval:5.0
                                                   target:self
                                                 selector:@selector(callWaiter)
                                                 userInfo:nil repeats:YES];

这是 callWaiter 的代码

 -(void)callWaiter
{ 
@try

    {
        NSLog(@"current serverTime----1=%@",currentSeverTime);
        NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:@"%@table_update?tag=update&admin=%@&table=%@&time=%@",baseUrl,adminId,@"Waitercall",[currentSeverTime stringByReplacingOccurrencesOfString:@" " withString:@"%20"]]];
        JsonParse5 *js5=[[JsonParse5 alloc] init];
        if([[js5 fetchData:[NSData dataWithContentsOfURL:url]] count]>0)
        {
            UIAlertView *grAlert=[[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:@"%@%@",LSSTRING(@"Waiter call - please attend table #"),[[[js5 fetchData:[NSData dataWithContentsOfURL:url]] objectAtIndex:0] objectForKey:@"table_no"] ] message:nil delegate:self cancelButtonTitle:LSSTRING(@"OK") otherButtonTitles:nil];

            [grAlert show];
            grAlert.tag=1;
            [grAlert release];


        }
        else
        {
            NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
            // this is imporant - we set our input date format to match our input string
            // if format doesn't match you'll get nil from your string, so be careful
            [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
            NSDate *dateFromString = [[NSDate alloc] init];
            // voila!
            dateFromString = [dateFormatter dateFromString:currentSeverTime];
            NSDate *correctDate = [NSDate dateWithTimeInterval:5.0 sinceDate:dateFromString];
            currentSeverTime=[dateFormatter stringFromDate:correctDate];

            NSLog(@"current serverTime----2=%@",currentSeverTime);






        }

    }
    @catch (NSException *exception) {
        NSLog(@"exception=%@",exception);
    }


}

它第一次工作正常,而在“当前 serverTime----1”日志之前第二次崩溃。我在 currentSeverTime 中分配值如下。

currentSeverTime=@"2013-07-16 07:15:50";

请帮助我,谢谢。

4

1 回答 1

0

我认为原因是之后

currentSeverTime=[dateFormatter stringFromDate:correctDate];

currentSeverTime不保留,currentSeverTime到达方法结束时释放。并currentSeverTime成为一个疯狂的参考。它在第二次使用时导致 EXC_BAD_ACCESS。

您可以将currentSeverTime其作为属性来更正代码。在 .h 文件中:

@property (retain) NSString *currentSeverTime;

在 .m 文件中:

  1. 在 dealloc 方法中:

    [_currentSeverTime 发布];

  2. 以这种方式使用它:

    self.currentSeverTime=[dateFormatter stringFromDate:correctDate];

希望它有所帮助。

于 2013-07-16T14:32:17.220 回答