代码之家  ›  专栏  ›  技术社区  ›  Hussein Esmail

如何保存一个。如果使用filemanager连接,则在USB上使用Swift 3的txt文件?(macOS应用程序开发)

  •  2
  • Hussein Esmail  · 技术社区  · 8 年前

    这可能是基于上述问题: Read and write data from text file

    我想做的是,如果连接了某种类型的可移动卷,可以选择直接在驱动器上写入文件,如果可能的话,可以在该驱动器的某个子文件夹中写入文件。

    let fileManager = FileManager.default
    var Extension: String = "txt"
    let FileName: String = "TestFileName"
    var DocumentDirURL = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
    let fileURL = DocumentDirURL.appendingPathComponent(FileName).appendingPathExtension(Extension)
    print("FilePath: \(fileURL.path)")
    let DocumentIntro = "This text will be on the text file\n"
    do {
        // Write to the file
        try DocumentIntro.write(to: fileURL, atomically: true, encoding: String.Encoding.utf8)
    } catch let error as NSError {
        print("Failed writing to URL: \(fileURL), Error: " + error.localizedDescription)
    }
    var FileContents = "" // Used to store the file contents
    do {
        // Read the file contents
        FileContents = try String(contentsOf: fileURL)
    } catch let error as NSError 
        print("Failed reading from URL: \(fileURL), Error: " + error.localizedDescription)
    }
    
    1 回复  |  直到 8 年前
        1
  •  3
  •   dgatwood    8 年前

    好消息是我确实写了一些 documentation

    坏消息是,它是一个纯C框架(核心基础),这使得即使在Objective-C中也很难使用,速度要慢得多。