4

If you're used Cocoa for a while you're probably familiar with NSDateFormatter and NSNumberFormatter. They're handy for creating formatted display strings from dates and numbers, or for converting date or number strings into numeric values, while supporting different languages and locales.

A few weeks ago I stumbled on NSDateComponentsFormatter, which lets you create formatted time intervals like "4 hours, 37 minutes and 17 seconds." Pretty cool.

There's also the related NSDateIntervalFormatter, which creates strings by comparing 2 dates.

Then there are some REALLY obscure NSFormatter subclasses:

NSMassFormatter
NSByteCountFormatter
NSLengthFormatter
NSEnergyFormatter
NSPersonNameComponentsFormatter

EDIT:

From the comments, I've aded NSPersonNameComponentsFormatter.

Searching on "NS*Formatter" in the Xcode help system reveals most of these, but not all. (It looks like the help text has to be indexed correctly in order for searching to work, which is annoying.)

That brings the total I have been able to find to

NSDateIntervalFormatter    -Difference between 2 dates
NSDateComponentsFormatter  -NSDateComponents to/from string
NSDateFormatter            -Formats NSDates as strings
NSNumberFormatter          -Formats numbers as strings
NSMassFormatter            -Formats mass quantity as strings 
NSByteCountFormatter       -Formats byte counts in K, MB, GB, etc.
NSLengthFormatter          -Formats length values
NSEnergyFormatter          -Displays energy qualities in Joules or Calories
NSPersonNameComponentsFormatter - displays localized formatted names

Annoyingly, it looks like many of these formatters don't have a locale property, so it's not very easy to use them to create formatted strings in languages/locales other than the system's default locale. If I'm missing something, please tell me.

Does anybody else know of other formatters I'm missing? These are pretty obscure, but could save you a lot of time if you were to need them.

EDIT #2:

Question part 2: Is there a way to get output from the formatters that lack a locale property in locale's other than the system default locale? It seems silly that they don't ALL have and honor a locale property. It's pretty common to need to generate output for languages/locales other than the current locale.

Duncan C
  • 128,072
  • 22
  • 173
  • 272
  • 2
    The only one I could find that you haven't listed is `NSPersonNameComponentsFormatter` - "Use this class to create localized names when displaying person name information to the user." Although definitely interesting (+1), I'm not sure whether this question is strictly on topic (seems fairly broad) ... It's also worth noting that (I think) all the subclasses of `NSFormatter` are listed in [the docs](https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSFormatter_Class/index.html#//apple_ref/swift/cl/c:objc(cs)NSFormatter) (Under "Inherits From"). – Hamish Apr 10 '16 at 13:52
  • 1
    This question does seem a bit off. Especially since the 1st paragraph of the `NSFormatter` documentation gives you the list of all the subclasses (though only those with `NS` prefixes). – rmaddy Apr 10 '16 at 14:07
  • 1
    @rmaddy If you look in the "Inherits From" block, it includes all the ones in Cocoa, even outside of Foundation (`NS`). – Rob Napier Apr 10 '16 at 14:22
  • @RobNapier I'm looking at the docs in Xcode. For `NSFormatter`, the "Inherits From" simply shows `NSObject`. The online docs show a much fuller inheritance tree. – rmaddy Apr 10 '16 at 14:23
  • 2
    @rmaddy Bah! Xcode documentation! Who uses that? :D Seriously, I always use Dash or the web (usually Dash). I don't know why the Xcode docs don't list the subclasses; the website does. https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSFormatter_Class/index.html (Yet another reason for me to dislike the Xcode doc browser.) – Rob Napier Apr 10 '16 at 14:25
  • 1
    I realize that this question is not a great fit for the format of SO questions, but I thought it would be useful and informative enough that it was still valuable to the community. And yes, there is a real question in there: What formatters have I missed? originalUser added `NSPersonNameComponentsFormatter` to the list. – Duncan C Apr 10 '16 at 14:37
  • I disagree that this is off-topic. It is a factual question with a definitive answer. And it generated technical approaches for finding answers to similar problems in the future. (I believe you should open your "Question part 2" as a separate question, however.) – Rob Napier Apr 10 '16 at 14:51
  • @Rob, I just created a new question about locales based on your suggestion: http://stackoverflow.com/questions/36532331/how-do-you-specify-the-locale-language-for-an-nsformatter-that-doesnt-have-a-lo – Duncan C Apr 10 '16 at 16:25

2 Answers2

5

There's no need to search. The NSFormatter documentation lists all of its subclasses. Look at the top of the page, in the "inherits from" block.

enter image description here

Note that this information is not available in the Xcode 7.3 doc reader. It's only available in the online docs (or by using the excellent Dash reader).

Rob Napier
  • 286,113
  • 34
  • 456
  • 610
  • 2
    There is no "inherits from" section in the Xcode docs. I do see it when I look up the NSFormatter class reference on the web. Thanks for that. Very annoying that that section is missing from the Xcode version of the docs. – Duncan C Apr 10 '16 at 14:42
  • 2
    So what is a `DRMSFFormatter`? The class reference is uniquely unhelpful. It says. "Instances of DRMSFFormatter format the textual representation of cells that contain MSF objects and convert textual representations of msf values into MSF objects." Ok, so what is an MSF object? Might as well say "An NSFurbar object handles instances of fubar." – Duncan C Apr 10 '16 at 14:50
  • 1
    @DuncanC: https://developer.apple.com/library/mac/documentation/MusicAudio/Reference/DiscRecordingFrameworkRef/index.html. MSF is "Minutes, Seconds, Frames." – Rob Napier Apr 10 '16 at 14:52
  • Updated for future searchers who might be confused when they don't find it in the docs. Given that it's hidden well enough that at least two very experienced Cocoa devs didn't find it, I suspect other devs will have the same problem. – Rob Napier Apr 10 '16 at 15:05
  • @RobNapier Five minutes have passed since you posted that, so naturally Apple has purged that webpage. Here's a backup somebody put online: http://stpeterandpaul.ca/tiger/documentation/MusicAudio/Reference/DiscRecordingFrameworkRef/index.html#//apple_ref/doc/framework/discrecording – original_username Mar 08 '17 at 01:17
2

I went into

/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/System/Library/Frameworks

and then did

for header in **/*.h; do ack -o 'NSFormatter' "$header"; done

which gave me some interesting ones:

NSPersonNameComponentsFormatter
CNContactFormatter
CNPostalAddressFormatter
DRMSFFormatter
MKDistanceFormatter

Doing the same for iPhoneOS.sdk didn't turn up any new NSFormatter subclasses.

Tamás Zahola
  • 9,271
  • 4
  • 34
  • 46