[NSString stringWithFormat:]的崩溃问题

时间:2012-03-27 11:31:45

标签: ios stringwithformat

我将我的申请提交给苹果审核。苹果拒绝并给了我以下崩溃报告。

Thread 0 Crashed:
1   Foundation                      0x3595dd04 _NSDescriptionWithLocaleFunc + 44
2   CoreFoundation                  0x36ae696e __CFStringAppendFormatCore + 7998
3   CoreFoundation                  0x36a611d8 _CFStringCreateWithFormatAndArgumentsAux + 68
4   Foundation                      0x3595dc2e +[NSString stringWithFormat:] + 54
5   WIPTime                         0x0001572e -[NSDate(TKCategory) reportHeader] + 66
6   WIPTime                         0x0006bd9e -[ReportDelegate tableView:cellForRowAtIndexPath:] + 474
7   UIKit                           0x36de009c -[UITableView(UITableViewInternal) _createPreparedCellForGlobalRow:withIndexPath:] + 540
8   UIKit                           0x36ddf17a -[UITableView(_UITableViewPrivate) _updateVisibleCellsNow:] + 1070
9   UIKit                           0x36dde904 -[UITableView layoutSubviews] + 200
10  UIKit                           0x36d830d8 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 176
11  CoreFoundation                  0x36a651f4 -[NSObject performSelector:withObject:] + 36
12  QuartzCore                      0x36241a9e -[CALayer layoutSublayers] + 210
13  QuartzCore                      0x362416b6 CA::Layer::layout_if_needed(CA::Transaction*) + 210
14  QuartzCore                      0x3624583c CA::Context::commit_transaction(CA::Transaction*) + 220
15  QuartzCore                      0x36245578 CA::Transaction::commit() + 308
16  QuartzCore                      0x3623d4b2 CA::Transaction::observer_callback(__CFRunLoopObserver*, unsigned long, void*) + 50
17  CoreFoundation                  0x36adab14 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 12
18  CoreFoundation                  0x36ad8d50 __CFRunLoopDoObservers + 252
19  CoreFoundation                  0x36ad90aa __CFRunLoopRun + 754
20  CoreFoundation                  0x36a5c49e CFRunLoopRunSpecific + 294
21  CoreFoundation                  0x36a5c366 CFRunLoopRunInMode + 98
22  GraphicsServices                0x30b3e432 GSEventRunModal + 130
23  UIKit                           0x36dade76 UIApplicationMain + 1074
24  WIPTime                         0x00003032 main + 42
25  WIPTime                         0x00002ffc start + 32

reportHeader的实现如下:

  -(NSString *) reportHeader
    {
    NSString *realWorld=[self dateDescriptionRealWorld];
    NSString *year=[self yearString];
    NSString *weekDay=[self weekdayString];
    NSString *returnString=[NSString stringWithFormat:@"%@ %@ %@",weekDay,realWorld,year];

    return returnString;
    }

该方法中使用的实用方法如下:

-(NSString *) dateDescriptionRealWorld
{

    return [NSString stringWithFormat:@"%@ %@",[self dayString],[self monthStringWithCod]];
}

- (NSString*) monthString{
    NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];  
    [dateFormatter setDateFormat:@"MMMM"];
    return [dateFormatter stringFromDate:self];
}

- (NSString*) yearString{
    NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];  
    [dateFormatter setDateFormat:@"yyyy"];
    return [dateFormatter stringFromDate:self];
}

- (NSString*) dayString{
    NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];  
    [dateFormatter setDateFormat:@"dd"];
    return [dateFormatter stringFromDate:self];
}

- (int) weekday{

    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *comps = [gregorian components:(NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit | NSWeekdayCalendarUnit) fromDate:self];
    int weekday = [comps weekday];
    [gregorian release];
    return weekday;
}

-(NSString *) weekdayString
{
    NSDate *date=self;
    int weekday=[date weekday];
    if (weekday ==0) {
        return @"Sat";
    }
    else if(weekday ==1)
    {
        return @"Sun";
    }
    else if(weekday ==2)
    {
        return @"Mon";

    }
    else if(weekday ==3)
    {
        return @"Tue";

    }
    else if(weekday ==4)
    {
        return @"Wed";

    }
    else if(weekday ==5)
    {
        return @"Thu";

    }
    else if(weekday ==5)
    {
        return @"Fri";

    }

}

2 个答案:

答案 0 :(得分:0)

变量realWorld在哪里出现?我怀疑这是一个类字段,你分配调用dateDescriptionRealWorld的结果,但从不保留。但这只是一个猜测,基于不完整的信息。

答案 1 :(得分:0)

一些想法......您应该使用switch / case和默认情况而不是if / else。此外,您正在错误地命名您的方法。而不是工作日方法,你应该在它做什么之后命名并返回,比如,weekdayFormatted。此外,您可能不应该在每个方法中分配新的dateFormatter,而是在您的类中使用一个NSDateFormatter,并在每个方法中设置其格式。

我建议删除所有方法并将格式放在reportHeader方法中,以进行清理。你正在分配相当多的东西,虽然你使用自动释放,但你依赖它太多来清理否则可能是不明智的内存管理。

这对你来说可能不是太“实用”的快速解决方案,但这些是我看到的可以带来更好的内存管理的东西。

另外 - @Aadihira有一个很好的问题,你也应该显示monthStringWithCod方法。