Re: Why would -[NSString dataUsingEncoding:allowLossyConversion:] throw exception?
Re: Why would -[NSString dataUsingEncoding:allowLossyConversion:] throw exception?
- Subject: Re: Why would -[NSString dataUsingEncoding:allowLossyConversion:] throw exception?
- From: "Adam R. Maxwell" <email@hidden>
- Date: Mon, 8 Jan 2007 19:52:20 -0800
On Jan 8, 2007, at 15:04, Sherm Pendley wrote:
On Jan 8, 2007, at 5:10 PM, Dan Wood wrote:
I'm trying to downsample a string to pure ASCII using the -
[NSString dataUsingEncoding:allowLossyConversion:] like this:
NSData *asciiData = [theString
dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
However, in some circumstances I get an exception thrown:
*** -[NSCFString dataUsingEncoding:allowLossyConversion:]: didn't
convert all characters
For instance, when I pass in the string "Det er gratis…" (the last
character is U+2026) it happens.
I'm not finding any documentation that it might throw an exception.
No, it's not documented that I can find. I'd file a docs bug.
That sounds more like a code bug, unless it's somehow using a
deprecated -cString method (which is documented to raise this). I
can't reproduce it with [NSString stringWithFormat:@"Det er gratis%C",
0x2026], and we used to use this technique to remove diacritics from
characters all the time.
Isn't the point of passing in YES so that you can still get
something back even if not all characters are converted?
Yes, but I can see why it might be useful to be notified when
something got lost in translation, and this method predates the
NSError class that's in vogue these days. You could catch and handle
the exception easily enough, or avoid it entirely with -
canBeConvertedToEncoding:.
But if you want to know if something will be lost, you should pass NO
and the method returns nil, right? Using -canBeConvertedToEncoding
when the whole point is to lose data probably won't help, either :).
-- adam_______________________________________________
Cocoa-dev mailing list (email@hidden)
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