1

EDIT: The questions linked to in the duplicate section answer this question quite well (I just hadn't found them when I posted this). Please refer to those.

I have just started looking into Swift (as of about three or four days ago), and have encountered a problem that I really do not understand, and for which I could not find anything already written about. I had already seen Read and write data from text file, and borrowed some of the code from the accepted answer there, but I'm still left with a question.

I was having some file IO trouble elsewhere, so in order to experiment and make sure I understood everything, I created a brand new single view app for iOS 10, to experiment with. All I have added is the following to the viewDidLoad() function in the view controller (sorry it is a bit messy):

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    let mydata = "I like to eat food!".data(using: .utf8)
    let mydata2 = "Tigers are a man's best friend".data(using: .utf8)

    let dir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first! as URL
    let fURL = dir.appendingPathComponent("hello.txt", isDirectory: false)

    NSLog(fURL.absoluteString)

    do {
        if !FileManager.default.fileExists(atPath: fURL.absoluteString) {
            let createdFile = FileManager.default.createFile(atPath: fURL.absoluteString, contents: mydata2!, attributes: nil)
            NSLog("File created? \(createdFile)")
        }

        try "The other string!".write(to: fURL, atomically: true, encoding: .utf8)

        if !FileManager.default.fileExists(atPath: fURL.absoluteString) {
            let createdFile = FileManager.default.createFile(atPath: fURL.absoluteString, contents: mydata2, attributes: nil)
            NSLog("File created? \(createdFile)")
        }

        try mydata2?.write(to: fURL)

        let filehandle = try FileHandle.init(forWritingTo: fURL)
        filehandle.truncateFile(atOffset: 0)
        filehandle.write(mydata!)

    } catch {
        NSLog(error.localizedDescription)
    }
}

I then run that through Xcode 8's simulator, and watch the documents folder for the app. The lines which use a write() function appear to work fine. A .txt file named hello.txt is created, and filled then re-filled with the data that is written out. The createFile lines however appear to fail, with createdFile always coming out as false, and no file appearing in the directory. The obvious difference between them is that the createFile lines use fURL.absoluteString, rather than the URL - unfortunately there doesn't seem to be a function in FileManager to use the URL as of right now.

When I tried using the absoluteString with one of the write functions, I got an error informing me that it failed because the folder "hello.txt" doesn't exist, which leaves me thoroughly confused because I don't quite follow how the code thinks that is supposed to be a directory, especially when use the isDirectory

So, clearly I am not understanding something and doing something wrong, but I can't see what. Can someone please explain the correct approach if I want to simply create a file ready for future write(s)? I should be able to use the fileHandle approach, but it seems strange to me that the other shouldn't work.

Community
  • 1
  • 1
Jarak
  • 972
  • 9
  • 16

0 Answers0