Re: Opening old text files with non-deprecated stringWithContentsOfFile calls?

Joar Wingfors <[email protected]> Tue, 12 Oct 2010 08:54:13 -0700
Newsgroups gmane.comp.macosx.devel
Message-ID <[email protected]>
On 12 okt 2010, at 07.44, Michael Dupuis wrote:

> Any of the files downloaded here will cause issues: http://www.grassrootsrecipes.com/
> 
> When I open them in TextWrangler, it appears that these are "Western  (MacOS) with Windows (CRLF), at least that is what's displayed at the bottom. If I just download one and try and read it in even using the
> 
> + (id)stringWithContentsOfFile:(NSString *)path usedEncoding:(NSStringEncoding *)enc error:(NSError **)error
> 
> call, it will fail. If I fallback to using the deprecated call, I can open it, so the deprecated call is doing SOMETHING that works, I just haven't been able to figure out what that is.


Don't use that method to initialize the string, use:

	+ (id)stringWithContentsOfFile:(NSString *)path encoding:(NSStringEncoding)enc error:(NSError **)error

The method you use tries to figure out the encoding used - an impossible task for the most part.

The deprecation of initializers was intended to force the developer to provide an encoding to use - so you need to do that. If you have reason to believe that these strings are encoded in "Western (MacOS)", then provide that (I think that's NSISOLatin1StringEncoding).

j o a r