• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Convert to NSString from
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Convert to NSString from


  • Subject: Re: Convert to NSString from
  • From: Jens Alfke <email@hidden>
  • Date: Fri, 06 Feb 2015 10:04:52 -0800

> On Feb 6, 2015, at 9:48 AM, Raglan T. Tiger <email@hidden> wrote:
>
> What is the 'best practice' for converting to NSString?

The best practice is to know how the string data was encoded, and pass that encoding to the NSString initializer. There isn't any single encoding constant that's best, because different data formats use different string encodings.

(A special case of this is filesystem paths — for these you should use the special NSString methods to convert to/from them. These are basically UTF-8 but with some specific details about normalization, and NSString knows about those details.)

If you really don't have any idea what the encoding is, the best heuristic is:
(1) Try UTF-8. If it succeeds, use that.
(2) If UTF-8 fails, fall back to WinLatin1, aka CP-1252, aka NSWindowsCP1252StringEncoding. This will always succeed.
(3) Never, ever use NSMacOSRomanStringEncoding … unless you're trying to open a file created on the 'classic' Mac OS (≤ 9) or by an app that weirdly uses that encoding (some old Carbon versions of Excel, I've heard.)

The rationale is that:
* All plain ASCII text will decode fine as any of these encodings, since they're all supersets of it.
* Most modern code uses UTF-8.
* Non-ASCII non-UTF-8 string data will typically fail to parse as UTF-8 because it contains invalid byte sequences.
* Older code tends to use ISO-Latin-1 or the default Windows encoding (WinLatin1).
* Fortunately WinLatin1 is a superset of ISO-Latin-1 so you can just try the latter.
* No one's used MacRoman encoding since the OS X transition, except for a few slow-moving legacy codebases.

—Jens
_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden


  • Follow-Ups:
    • Re: Convert to NSString from
      • From: Fritz Anderson <email@hidden>
References: 
 >Convert to NSString from (From: "Raglan T. Tiger" <email@hidden>)

  • Prev by Date: Convert to NSString from
  • Next by Date: Re: ARC dealloc best pratice
  • Previous by thread: Convert to NSString from
  • Next by thread: Re: Convert to NSString from
  • Index(es):
    • Date
    • Thread