我想使用NSDateFormatter格式化日期,但我的应用程序崩溃与EXC_BAD_ACCESS。NSDateFormatter崩溃
下面的代码:
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];
上面我碰到了两个自定义的UITableViewCell我有日期的代码。值不为空(我测试过)。但是,当我尝试从我的Helpers类使用我的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;
}
的自定义的UITableViewCell的NSDateFormatter这样定义:
self.dateFormatter = [[NSDateFormatter alloc] init];
self.dateFormatter.timeStyle = NSDateFormatterNoStyle;
self.dateFormatter.dateStyle = NSDateFormatterMediumStyle;
我看到了一些类似的问题在这里,但未能解决这个问题!
在此先感谢!
编辑
包括崩溃日志可能会帮助... –
哪里是你的''中的方法defaultFormatter'声明return'?设置这些值后,您应该返回'defaultFormatter'。 – iDev
我忘了包含return语句!代码编辑! – CainaSouza