To read a file into a string, I'm using the following:
let contents = try String(contentsOf: url)
I'm not specifying a particular encoding because, of course, the user could open any file with any encoding. Problem is, this works for some encodings and not others. Using file -I *
in my directory or files, I realised that the files I'm trying this on are a mixture of us-ascii
and iso-8859-1
files. When it attempts to read us-ascii
files, it works, but it doesn't for iso-8859-1
.
I suppose iso-8859-1
is less common or something, because Swift doesn't seem to be able to infer its encoding for me. Is there some way to make this work? Maybe using a library to detect encoding?