Ios NSDateFormatter崩溃
我正在尝试使用NSDateFormatter格式化日期,但我的应用程序因EXC_BAD_访问而崩溃 代码如下:Ios NSDateFormatter崩溃,ios,objective-c,nsdate,nsdateformatter,Ios,Objective C,Nsdate,Nsdateformatter,我正在尝试使用NSDateFormatter格式化日期,但我的应用程序因EXC_BAD_访问而崩溃 代码如下: DateInputTableViewCell *cell0 = (DateInputTableViewCell *)[paramTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]]; DateInputTableViewCell *cell1 = (DateInputTableViewC
DateInputTableViewCell *cell0 = (DateInputTableViewCell *)[paramTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
DateInputTableViewCell *cell1 = (DateInputTableViewCell *)[paramTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]];
NSString *fromDate = [[Helpers defaultFormatter] stringFromDate:cell0.dateValue];
NSString *toDate = [[Helpers defaultFormatter] stringFromDate:cell1.dateValue];
+ (NSDateFormatter *)defaultFormatter{
defaultFormatter = [[NSDateFormatter alloc] init];
NSLocale* locale = [[NSLocale alloc] initWithLocaleIdentifier:@"pt_BR"];
[defaultFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"America/Sao_Paulo"]];
[defaultFormatter setDateFormat:@"yyyy'-'MM'-'dd' 'HH':'mm':'ss"];
[defaultFormatter setLocale:locale];
[locale release];
return defaultFormatter;
}
上面的代码是从我拥有的两个自定义UITableViewCell获取日期。这些值不是空的(我已经测试过)。但是当我尝试从Helpers类使用defaultFormatter方法时,应用程序崩溃了。代码如下:
DateInputTableViewCell *cell0 = (DateInputTableViewCell *)[paramTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
DateInputTableViewCell *cell1 = (DateInputTableViewCell *)[paramTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]];
NSString *fromDate = [[Helpers defaultFormatter] stringFromDate:cell0.dateValue];
NSString *toDate = [[Helpers defaultFormatter] stringFromDate:cell1.dateValue];
+ (NSDateFormatter *)defaultFormatter{
defaultFormatter = [[NSDateFormatter alloc] init];
NSLocale* locale = [[NSLocale alloc] initWithLocaleIdentifier:@"pt_BR"];
[defaultFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"America/Sao_Paulo"]];
[defaultFormatter setDateFormat:@"yyyy'-'MM'-'dd' 'HH':'mm':'ss"];
[defaultFormatter setLocale:locale];
[locale release];
return defaultFormatter;
}
自定义UITableViewCell的NSDateFormatter定义如下:
self.dateFormatter = [[NSDateFormatter alloc] init];
self.dateFormatter.timeStyle = NSDateFormatterNoStyle;
self.dateFormatter.dateStyle = NSDateFormatterMediumStyle;
我在这里看到了一些类似的问题,但无法解决此问题
提前谢谢
编辑
您的
defaultFormatter
方法没有返回任何内容,因此您会得到一个垃圾值,您试图将导致崩溃的stringFromDate:
消息发送到该值。编译器应该警告您这一点,所以请始终修复编译器警告 正如我在评论中提到的,return
语句在defaultFormatter
方法中丢失。设置值后,应返回defaultFormatter
应该是这样,
+ (NSDateFormatter *)defaultFormatter{
defaultFormatter = [[NSDateFormatter alloc] init];
NSLocale* locale = [[NSLocale alloc] initWithLocaleIdentifier:@"pt_BR"];
[defaultFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"America/Sao_Paulo"]];
[defaultFormatter setDateFormat:@"yyyy'-'MM'-'dd' 'HH':'mm':'ss"];
[defaultFormatter setLocale:locale];
[locale release];
return defaultFormatter;
}
包含崩溃日志可能会有所帮助…您的
return
语句在defaultFormatter
方法中的哪里?设置这些值后,您应该返回defaultFormatter
。我忘记包含return语句了!代码编辑!我在崩溃日志中包含了一个图像。defaultFormatter
声明为您的代码中的@属性
?否,它是静态的,作为static NSDateFormatter*defaultFormatter代码>为什么不尝试为defaultFormatter
静态参数使用不同的名称,因为该方法也有相同的名称。如果在助手类中将其声明为normal@property呢?它仍在崩溃吗?无法声明为@property,因为我有类方法(+(NSDateFormatter*)defaultFormatter
)。我已经在我的应用程序的其他类中使用了此方法。只有这个特定的类正在崩溃。。。