'init(contentsOfFile:)' was deprecated in iOS 18

content = try String(contentsOfFile: filepath) is the following warning.

'init(contentsOfFile:)' was deprecated in iOS 18: Use init(contentsOfFile:encoding:) instead

After internet searching I could find an answer.

I would appreciate a rewrite of my code line to get rid of 31 warnings.

Thanks a LOT

Charlie

Answered by RickMaddy in 850847022

What issue are you having with the change? The deprecation warning that you quoted in your question tells you what the replacement is. In short, add the encoding parameter.

What issue are you having with the change? The deprecation warning that you quoted in your question tells you what the replacement is. In short, add the encoding parameter.

What RickMaddy said plus…

The reason why init(contentsOfFile:) is deprecated in favour of init(contentsOfFile:encoding:) is that the latter takes a text encoding parameter, whereas the former has to guess what text encoding to use. Guessing text encodings is hard, and thus it’s better for you to tell the system what encoding to use.

If you don’t have any advanced knowledge about the text encoding, the standard option on Apple platforms is UTF-8, and thus you should pass in .utf8.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Tried this and got "encoding not is scope"

content = try String (self.init contentsOfFile:encoding: .utf8)

Thanks Quinn

Tried a different sequence and got "encoding not is scope"

content = try String (contentsOfFile: encoding: utf16: filepath)

Charlie

You had this:

content = try String(contentsOfFile: filepath) 

You need this now:

content = try String(contentsOfFile: filepath, encoding: .utf8) 

As I said, all you need to do is add the additiional encoding parameter.

Thanks Rick Maddy

I had the filepath in the wrong place.
31 warnings disappeared. YEA

Charlie

'init(contentsOfFile:)' was deprecated in iOS 18
 
 
Q