I am saving files in a document directory in swift 3 with this code:
fileManager = FileManager.default
// let documentDirectory = fileManager?.urls(for: .documentDirectory, in: .userDomainMask).first as String
var path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String
path = path + name
let image = #imageLiteral(resourceName: "Notifications")
let imageData = UIImageJPEGRepresentation(image, 0.5)
let bool = fileManager?.createFile(atPath: path, contents: imageData, attributes: nil)
print("bool is \(bool)")
return true
But as you can see, I am not using filemanager
to get document directory path as filemanager
gives only URL not string.
Questions:
- How to get string from file manager?
- Is there any chance of crash in my code?