Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String.init(contentsOfFile:) replacement for Linux?

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?

like image 969
emlai Avatar asked Dec 15 '22 01:12

emlai


1 Answers

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.

like image 98
OOPer Avatar answered Dec 16 '22 13:12

OOPer