Swift Read File

[Solved] Swift Read File | Swift - Code Explorer | yomemimo.com
Question : swift read file

Answered by : happy-hyena-j4t6squdpbx2

let file = "file.txt" //this is the file. we will write to and read from it
let text = "some text" //just a text
if let dir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first { let fileURL = dir.appendingPathComponent(file) //writing do { try text.write(to: fileURL, atomically: false, encoding: .utf8) } catch {/* error handling here */} //reading do { let text2 = try String(contentsOf: fileURL, encoding: .utf8) } catch {/* error handling here */}
}

Source : https://stackoverflow.com/questions/24097826/read-and-write-a-string-from-text-file | Last Update : Sat, 12 Jun 21

Answers related to swift read file

Code Explorer Popular Question For Swift