function writeFile()

I'v been trying to write a document directory file but I can't find the file anywhere. ( Terminal comand: find . -name "blw*"). Part of the ContentView.swift looks like this.

func writeFile() {        let file = "blw.txt"        let text = "hakemisto"        let documentDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!                 print("documentDir: (documentDirectory)")            if let dir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first {        let fileURL = dir.appendingPathComponent(file)        do {            try text.write(to: fileURL, atomically: false, encoding: .utf8)        } catch {          }                }   I don't understand what's wrong. That print does not print anything so I guess that whole if section is ignored.

MacOS 12.0.1 XCode 13.1

You should format code with formatter (<>) to make it readable:

And put statements to see what's going on:

func writeFile() {
    let file = "blw.txt"
    let text = "hakemisto"
    let documentDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
    print("documentDir: (documentDirectory)", FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first)    // <<-- To check if not nil
    if let dir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first {
        let fileURL = dir.appendingPathComponent(file)
        do {
            try text.write(to: fileURL, atomically: false, encoding: .utf8)
        } catch {
             print("Failed to write text: \(error.localizedDescription)")
        }
    }
}

You may have a sandbox issue. You need to use NSSavePanel() to access the file.

function writeFile()
 
 
Q