Save file to document directory in fast 3?

I save files in the 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 do not use filemanager to get the path to the document directory, since filemanager only specifies a URL, not a string.

Questions:

  • How to get a line from the file manager?
  • Is there any chance of a crash in my code?
+23
ios iphone swift nsfilemanager nsdocumentdirectory
source share
5 answers

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 
+59
source share

following the vadian example above, the only line you need to save the (Data) file in the document directory is:

try imageData.write (to: fileURL)

Getting the file path is an interesting part

ex: create file path

  func createNewDirPath( )->URL{ let dirPathNoScheme = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String //add directory path file Scheme; some operations fail w/out it let dirPath = "file://\(dirPathNoScheme)" //name your file, make sure you get the ext right .mp3/.wav/.m4a/.mov/.whatever let fileName = "thisIsYourFileName.mov" let pathArray = [dirPath, fileName] let path = URL(string: pathArray.joined(separator: "/")) //use a guard since the result is an optional guard let filePath = path else { //if it fails do this stuff: return URL(string: "choose how to handle error here")! } //if it works return the filePath return filePath } 

function call:

 let shinnyNewURLpath = createNewDirPath( ) //write data to file using one line in do/catch operation do { try yourDataObject?.write(to: shinnyNewURLpath) } catch { print("catch error",error.localizedDescription) } 
+3
source share

I use the following method to create the file "Test.txt". Hope this helps you.

 func createFile() { let fileName = "Test" let documentDirURL = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true) let fileURL = documentDirURL.appendingPathComponent(fileName).appendingPathExtension("txt") print("File PAth: \(fileURL.path)") } 
0
source share

How to create a folder and files on the desktop or in a specific location on MacOS in Swift.

0
source share

func copyandpast () {

  var path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true); let dbpath :NSString = path[0] as NSString; let strdbpath = dbpath.strings(byAppendingPaths: ["mydb.db"])[0] ; print(strdbpath); let fmnager = FileManager.default; if !fmnager.fileExists(atPath: strdbpath) { let local = Bundle.main.path(forResource: "mydb", ofType: "db"); do { try fmnager.copyItem(atPath: local!, toPath: strdbpath) }catch{ } } } 
-eleven
source share

All Articles