I manually created a file amutha.txt
in the documents folder. I tried to write data to that file. code which I used is
let string="Amuthapriya"
try string.write(to:fileName, atomically: true, encoding: String.Encoding.utf8)
This is executed correctly means having no exceptions or errors. But then When I open amutha.txt the file is empty. Why the string is not written in that file? What Mistake I am doing? My code is:
func getDocumentsDirectory() -> URL {
let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
let documentsDirectory = paths[0]
return documentsDirectory
}
@objc func buttonPressed(sender:UIButton) {
print(sender.titleLabel?.text)
let documentUrl:URL = getDocumentsDirectory()
let fileName=documentUrl.appendingPathComponent("priya.txt", isDirectory: false)
let filePath=fileName.path
print( FileManager.default.fileExists(atPath: filePath))
do {
let string="Amuthapriya"
try string.write(to:fileName, atomically: true, encoding: String.Encoding.utf8)
print("written successfully")
print("filePath: \(filePath)")
} catch {
}
}