Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6051825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:51:37+00:00 2026-05-23T07:51:37+00:00

My team found that we were using a variety of NSDateFormatter objects throughout our

  • 0

My team found that we were using a variety of NSDateFormatter objects throughout our code base, and started looking into how we could avoid the cost/confusion of allocating/initializing common formatters in a bunch of separate places.

One idea we had was to create a category on the NSDateFormatter class that would provide a reference to a static instance of a commonly configured formatter. For example, we were using the “short time” date formatter in several places, and were looking to add the following class method:

@implementation NSDateFormatter (NSDateFormatter_PDDateFormatters)

static NSDateFormatter * shortTimeFormatter = nil;

+ (NSDateFormatter *) PDSharedShortTimeFormatter {

    @synchronized([NSDateFormatter class]){

        if( shortTimeFormatter == nil){

           // Create new formatter for SHORT times (e.g. 12:00 pm)

           shortTimeFormatter = [[NSDateFormatter alloc] init];
           [shortTimeFormatter setDateStyle: NSDateFormatterNoStyle];
           [shortTimeFormatter setTimeStyle:NSDateFormatterShortStyle];
       }

      return shortTimeFormatter;

   }

  return nil;
}

@end

One of the issues I have with this approach is that we are not currently “protecting” the NSDateFormatter from being changed. Since the formatter is essentially “shared” throughout our application, this could potentially cause problems if another object was to change the formatter’s configuration (e.g. time/date style).

Because we are using this internally, I’m not overly concerned with the risk of our team misusing this functionality (i.e. it’s a small team, and clearly commented).

However, I was wondering about best practices here.

Is there a way to return an immutable reference to the date formatter? If I return a copy of a formatter, is that any less expensive than doing the alloc/inits that we’re doing now?

Is there some other approach to take here?

We’ll be up and running with this, but it’s always good to get some feedback in writing “better” code.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T07:51:38+00:00Added an answer on May 23, 2026 at 7:51 am

    Normally, you would just not worry about it. Obj-C will let you fiddle with the juicy insides of almost anything. Even @private doesn’t protect against -valueForKey:_thatFunPrivateIvar. And if all else fails, you can just invoke runtime functions.

    However, the simplest workaround here would be to expose an API that internally uses cached formatters, but that provides no access to the formatters it is using. Your code would then use +[Formatter shortTimeStringFromDate:] to do what your sample code is doing now. The formatter in question could be lazily allocated, and you could use purgeable memory so cached formatters could be cleared in a LRU fashion under memory pressure.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I found the following code in my team's project: Public Shared Function isRemoteDisconnectMessage(ByRef m
I've been using MSTest so far for my unit-tests, and found that it would
I see that the IronRuby team has documented using ActiveRecord in IronRuby with MSSQL
I'm part of a development team that works on many CMS based projects, using
I'm working with a team that's building an engine for a variety of 2D
I am using WPF, and attempting to follow the MVVM pattern. Our team has
I found a number of ways to automatically increment build numbers with Team build
I've moved to a new project team and while going over the codebase, found
Team Foundation Server 2008 allows that every check-in is associated with a work item,
Our team is now beginning to look at jumping from 2.0 to 3.5 and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.