> On Feb 6, 2015, at 9:48 AM, Raglan T. Tiger <r...@crusaderrabbit.net> 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 (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to