-(NSString*) convertToDateString:(NSString*)str{
static NSDateFormatter *InDateFormatter = nil;
static NSDateFormatter *OutDateFormatter = nil;
NSString *outDate = nil;
if([str isKindOfClass:[NSString class]] && [str length] > 0){
NSString *dateStr = [[NSString alloc] initWithString:str];
if (InDateFormatter == nil) {
InDateFormatter = [[NSDateFormatter alloc] init];
InDateFormatter.dateFormat = @"yyyy-MM-dd";
}
if (OutDateFormatter == nil) {
OutDateFormatter = [[NSDateFormatter alloc] init];
OutDateFormatter.dateFormat = @"MM/dd/yy";
}
NSRange rangeOfDash = [dateStr rangeOfString:@"T"];
dateStr = (rangeOfDash.location != NSNotFound) ? [dateStr substringToIndex:rangeOfDash.location] : dateStr;
if([dateStr isKindOfClass:[NSString class]] && [dateStr length] == 10){
NSDate* date = [InDateFormatter dateFromString:dateStr];
if (date != nil) {
outDate = [OutDateFormatter stringFromDate:date];
}
}
}
return outDate;
}
崩溃发生在 "InDateFormatter.dateFormat = @"yyyy-MM-dd"; 行将有多个并行创建的类实例并调用上述方法。崩溃很少可重现。
我没有正确使用日期格式化程序吗?提前致谢