Please think the other way around.
URL is the recommended way to handle file paths, because it contains all the convenient methods for adding and removing components and path extensions, not the String where Apple removed these methods.
You are not recommended to combine paths such as path = path + name . This is error prone because you are responsible for all slash path separators.
Next, you do not need to create a file using FileManager . Data is a method for writing data to disk.
let fileManager = FileManager.default do { let documentDirectory = try fileManager.url(for: .documentDirectory, in: .userDomainMask, appropriateFor:nil, create:false) let fileURL = documentDirectory.appendingPathComponent(name) let image = #imageLiteral(resourceName: "Notifications") if let imageData = UIImageJPEGRepresentation(image, 0.5) { try imageData.write(to: fileURL) return true } } catch { print(error) } return false
vadian
source share