在SWIFT 5中创建和写入子目录时出现问题

Problem creating and writing a subdirectory in Swift 5(在SWIFT 5中创建和写入子目录时出现问题)

本文介绍了在SWIFT 5中创建和写入子目录时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试将文本文件写入我的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 = "
Title: " + titleString + "

"
          let tempDate: String = "Date: " + dateString + "

"
          let tempRecurring: String = "Recurs: " + tempRecurrance + "



"
        
          
          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"}

推荐答案

如果文件已存在,请取消选中if !fileManager.fileExists(atPath: myAppDirectoryURL.path),因为createDirectory调用不会在withIntermediateDirectories设置为True时生成错误。

您使用的一些属性不是问题的一部分,因此以下是我对您的函数的完整性测试版本

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)
    }
}

这篇关于在SWIFT 5中创建和写入子目录时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:在SWIFT 5中创建和写入子目录时出现问题

基础教程推荐