String.init(contentsOfFile:) replacement for Linux?

978 Views Asked by At

After deploying my Swift 3 app to Heroku, it crashed with the following error:

fatal error: init(contentsOfFile:usedEncoding:) is not yet implemented: file Foundation/NSString.swift, line 1255

What can I use instead of String.init(contentsOfFile:) on Ubuntu?

1

There are 1 best solutions below

0
On BEST ANSWER

Seeing the latest source code of Swift Standard Library, String.init(contentsOfFile:) internally calls NSString.init(contentsOfFile:usedEncoding:). (NSStringAPI.swift)

And the Linux version of NSString.init(contentsOfFile:usedEncoding:), as you see, is not implemented yet. (NSString.swift)

Seems NSString.init(contentsOfFile:encoding:) is already implemented and String.init(contentsOfFile:encoding:) calls it. So, if you know the encoding of the file, use String.init(contentsOfFile:encoding:) like:

let fileContent =  try? String(contentsOfFile: filePath, encoding: .utf8)

If you do not know the string encoding of the file, you may need to implement the functionality by yourself.