Re: stringWithContentsOfFile:usedEncoding returns no err and no encoding
Re: stringWithContentsOfFile:usedEncoding returns no err and no encoding
- Subject: Re: stringWithContentsOfFile:usedEncoding returns no err and no encoding
- From: Greg Parker <email@hidden>
- Date: Fri, 30 May 2014 22:08:43 -0700
On May 30, 2014, at 9:59 PM, Trygve Inda <email@hidden> wrote:
> I have a database file exported as text from FileMaker which has several
> high-ASCII French characters.
>
> NSError* error = nil;
> NSStringEncoding* enc = nil;
>
> NSString* contents = [NSString stringWithContentsOfFile:path
> usedEncoding:enc error:&error];
>
>
> This works but enc and error are both nil after the method returns, even
> though contents is correct.
>
> NSString* contents = [NSString stringWithContentsOfFile:path encoding:
> NSASCIIStringEncoding error:&error];
>
> This one returns no error but the high ASCII French acented chars are messed
> up.
>
> NSString* contents = [NSString stringWithContentsOfFile:path encoding:
> NSUTF8StringEncoding error:&error];
>
> This returns error 261 (file can't be interpreted).
>
> So why in the first example is it working (no error) but also not returning
> any encoding value?
You're calling -stringWithContentsOfFile:usedEncoding:error: incorrectly. Try this:
NSError *error = nil;
NSStringEncoding enc;
NSString* contents = [NSString stringWithContentsOfFile:path
usedEncoding:&enc error:&error];
Note that NSString may not be able to guess your file encoding correctly. If you can discover which encoding FileMaker actually uses you'll be better off. (MacRoman is a good guess.)
--
Greg Parker email@hidden Runtime Wrangler
_______________________________________________
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