Re: NSPropertyListSerialization and string encoding
Re: NSPropertyListSerialization and string encoding
- Subject: Re: NSPropertyListSerialization and string encoding
- From: Kyle Sluder <email@hidden>
- Date: Thu, 22 Jul 2010 19:49:46 -0700
On Thu, Jul 22, 2010 at 6:25 PM, Joe Turner <email@hidden> wrote:
> I'm having an issue with NSPropertyListSerialization, where I will call dataWithPropertyList:format:options:error:, to get the data from a property list–this part goes fine. However, then I need the data as a string, so I can upload it to a web service. However, when I call initWithData:encoding: to get a string (I'm using NSASCIIStringEncoding, but have tried many others as well), wherever there was a letter with an accent, weird stuff happens. Like é turns into Ä. I'm fine with losing accents, but right now, I can't even seem to do that–I just get the Ä's. Is there any way I can use a different encoding or something else to keep the accents for the time being, and then filter them out when I create the post data with dataUsingEncoding:allowLossyConversion:?
There are two potential encoding mismatches here:
1) The encoding of the serialized plist vs. the encoding you pass to
-initWithData:encoding:
2) The encoding of the data you send to the web service vs. the
encoding the service is expecting to receive
You need to make sure that both of these situations are handled correctly.
As James says, the "standard" (more accurately, "assumed by default")
encoding of XML is UTF-8. But you can do even better than that, if you
like, since the only logical choice for plist format in this case is
XML: you can check the encoding of the serialized plist by creating an
NSXMLDocument out of it and asking for its -characterEncoding. While I
doubt that NSPropertyListSerialization is ever going to start emitting
XML documents that are not UTF-8 encoded, this is the most correct
approach.
Either way, that gives you the answer for what encoding you should be
passing -initWithData:encoding:. But I'm kind of curious why you're
bothering to convert it to a string in the first place, since it can't
possibly be of any benefit to you. NSString's internal representation
is UTF-16 anyway, so the first thing -initWithData:encoding: is going
to do is convert your XML document (with its encoding="utf-8" header!)
into UTF-16. Then at some point down the line you'll need to ask for
the data back in UTF-8 format, or else you'll wind up with a lying
encoding directive.
Long story short, it sounds like you're abusing NSString. Data are not
the strings, and strings are not data; Cocoa is designed to keep the
two concepts separate.
--Kyle Sluder
_______________________________________________
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