【问题标题】:Problem creating and writing a subdirectory in Swift 5在 Swift 5 中创建和编写子目录时出现问题
【发布时间】:2021-10-14 20:03:15
【问题描述】:

我在尝试将文本文件写入我的 Documents 文件夹的子目录时遇到问题。

我的代码如下:

 @objc func makeFileButtonTapped() {
        print(#function)
        
        let tempName = "test"
        
    var tempRecurrance: String = ""
          
          switch recurrentInt {
          case 0:
              tempRecurrance = "Never"
              
          case 1:
              tempRecurrance = "Sometimes"
              
          case 2:
              tempRecurrance = "Often"
              
          default:
               tempRecurrance = "Unknown"
          }
          
          
          fileName = tempName + ".txt"
          
          let tempTitle: String = "\n\Title: " + titleString + "\n\n"
          let tempDate: String = "Date: " + dateString + "\n\n"
          let tempRecurring: String = "Recurs: " + tempRecurrance + "\n\n\n\n"
        
          
          myDocument = ""
          myDocument.append(tempTitle)
          myDocument.append(tempDate)
          myDocument.append(tempRecurring)
        
    
        saveToDirectory()
    }



func saveToDirectory(){
     print(#function)
        
        let fileManager = FileManager.default
       
        //  Create subdirectory
        do {
            
        let directoryURL = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first!
         let myAppDirectoryURL = directoryURL.appendingPathComponent("MySubDirectory")
        
        
        if !fileManager.fileExists(atPath: myAppDirectoryURL.path) {
            try fileManager.createDirectory(at: myAppDirectoryURL, withIntermediateDirectories: true, attributes: nil)
            print("New directory created.")
        
            //print("dictionary already exists.")
    }
        //  Create document
         let documentURL = myAppDirectoryURL.appendingPathComponent (fileName)
            
            try myDocument.write(to: documentURL, atomically: false, encoding: .utf8)

            print("documentURL =", documentURL.path)
        } catch {
            print(error)
        }
        
    }

我收到一条控制台消息,指出目录{Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}

【问题讨论】:

  • 这是 MacOS 还是 iOS?您的代码在哪里报告了错误?

标签: swift document


【解决方案1】:

如果文件已经存在,请取消检查,if !fileManager.fileExists(atPath: myAppDirectoryURL.path) 因为如果在将withIntermediateDirectories 设置为 true 时目录已经存在,createDirectory 调用将不会产生错误。

您使用了一些不属于问题的属性,因此为了完整性,这是我的功能测试版本

func saveToDirectory(_ fileName: String, text: String) {
    let fileManager = FileManager.default

    do {
        let directoryURL = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first!
        let myAppDirectoryURL = directoryURL.appendingPathComponent("MySubDirectory")
        try fileManager.createDirectory(at: myAppDirectoryURL, withIntermediateDirectories: true, attributes: nil)

        let documentURL = myAppDirectoryURL.appendingPathComponent (fileName)
        try text.write(to: documentURL, atomically: true, encoding: .utf8)
    } catch {
        print(error)
    }
}

【讨论】:

  • 非常感谢乔金。效果很好。
猜你喜欢
  • 1970-01-01
  • 2011-12-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-20
  • 2018-09-03
  • 2017-01-27
  • 1970-01-01
相关资源
最近更新 更多