开发者

setting format of ivar NSDateFormatter multiple times

开发者 https://www.devze.com 2023-03-26 21:23 出处:网络
In one of my view controllers, i have a lot of different formats for dates, so i decided to create an ivar in my header file

In one of my view controllers, i have a lot of different formats for dates, so i decided to create an ivar in my header file

NSDateFormatter *dateFormatter;

and then I use it with different formats throughout different methods in my implementation file

dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"EEEE, MMM dd"];
//do something with this
[dateFormatter setDateFormat:@"y-MM-dd HH:mm:ss"];
//do something with this
etc

Is this OK to do? is this what they mean by caching your nsdateformatters?

This seems to work fine when i just build=>run but when i enable zombies, my app crashes and the malloc_history shows a bunch of alloc/free with dateformat and other date related text. here's an example of an alloc

ALLOC 0x5e37930-0x5e3799b [size=108]: thread_accc52c0 |start | main | 
UIApplicationMain | GSEventRun | GSEventRunModal | CFRunLoopRunInMode | 
CFRunLoopRunSpecific | __CFRunLoopRun | __CFRunLoopDoObservers | 
__CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ | 
CA::Transaction::observer_callback(__CFRunLoopObserver*, unsigned long, void*) | 
CA::Transaction::commit() | CA::Context::commit_transaction(CA::Transaction*) | 
CALayerLayoutIfNeeded | -[CALayer layoutSublayers] | -[UITableView layoutSubviews]
 | -[UITableView(_UITabl开发者_如何学PythoneViewPrivate) _updateVisibleCellsNow:] | -
[UITableView(UITableViewInternal) _createPreparedCellForGlobalRow:] | -
[UITableView(UITableViewInternal) _createPreparedCellForGlobalRow:withIndexPath:] 
| -[EventsTableController tableView:cellForRowAtIndexPath:] | -[NSDateFormatter 
dateFromString:] | -[NSDateFormatter getObjectValue:forString:errorDescription:] 
| getObjectValue | CFDateFormatterCreateDateFromString | 
CFDateFormatterGetAbsoluteTimeFromString | udat_parse | 
icu::DateFormat::parse(icu::UnicodeString const&, icu::ParsePosition&) const | 
icu::SimpleDateFormat::parse(icu::UnicodeString const&, icu::Calendar&, 
icu::ParsePosition&) const | icu::SimpleDateFormat::subParse(icu::UnicodeString 
const&, int&, unsigned short, int, signed char, signed char, signed char*, int&, 
icu::Calendar&, int) const | icu::SimpleDateFormat::parseInt(icu::UnicodeString 
const&, icu::Formattable&, int, icu::ParsePosition&, signed char, 
icu::NumberFormat*) const | icu::DecimalFormat::parse(icu::UnicodeString const&, 
icu::Formattable&, icu::ParsePosition&) const | 
icu::DecimalFormat::parse(icu::UnicodeString const&, icu::Formattable&, 
icu::ParsePosition&, signed char) const | icu::DecimalFormat::subparse(icu::UnicodeString const&, 
icu::UnicodeString const*, icu::UnicodeString const*, icu::UnicodeString const*, 
icu::UnicodeString const*, signed char, signed char, icu::ParsePosition&, 
icu::DigitList&, signed char*, unsigned short*) const | 
icu::DecimalFormat::compareSimpleAffix(icu::UnicodeString const&, icu::UnicodeString 
const&, int, signed char) | uprv_decNumberInvert | uprv_decNumberInvert | 
icu::UnicodeSet::UnicodeSet(icu::UnicodeSet const&) | malloc | malloc_zone_malloc 

Thanks in advance


It's legitimate to have a single instance of NSDateFormatter and repeatedly change the format, so long as this is only done in one thread and you keep straight what you're doing.

But you should only create the NSDateFormatter once, in your init routine -- don't create it in each routine that uses it. And be sure to have a delete of the formatter in your dealloc routine.

0

精彩评论

暂无评论...
验证码 换一张
取 消