如何查找本地路径并将其与文件夹名称分开并获取那些对象的路径

时间:2018-11-16 14:36:03

标签: swift firebase core-data google-cloud-firestore local

我从firestore下载了文件,并将它们及其documentIds保存在本地。现在,我必须找到这些文件的路径,并使其具有文件夹名称(documentId)的json文件

This is my test folder,并且他们已经有ID。 我测试了一些这样的代码:

  let fileManager = FileManager.default
    let documentsPath1 = NSURL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0])
    let logsPath = (documentsPath1.appendingPathComponent("/testEmo/"))
    do
    {
        let fileURLs = try fileManager.contentsOfDirectory(at: logsPath!, includingPropertiesForKeys: nil)
        print("fileURLs \(fileURLs)")
 This is the printed path :  /Users/users/Library/Developer/CoreSimulator/Devices/deviceId/data/Containers/Data/Application/DeviceId/Documents/testEmo
    }
    catch let error as NSError
    {
        NSLog("Unable to create directory \(error.debugDescription)")
    }

如果我可以采用所有这些对象的路径,则将这些路径保存在json文本文件中以供应用读取。

找到了解决方案。 如果有人有兴趣;

do {
        let documentURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
        let Path = documentURL.appendingPathComponent("folderName").absoluteURL
        let directoryContents = try FileManager.default.contentsOfDirectory(at: Path, includingPropertiesForKeys: nil, options: [])
        for x in directoryContents {
            print("lastPathComponent \(x.lastPathComponent)")
            let path2 = Path.appendingPathComponent(x.lastPathComponent).absoluteURL
            let directoryContents2 = try FileManager.default.contentsOfDirectory(at: path2, includingPropertiesForKeys: nil, options: [])

            for x in directoryContents2 {
                print("checkdirectoryContents2 \(x.lastPathComponent)")
            }

        }

    }
    catch {
        print(error.localizedDescription)
    }

0 个答案:

没有答案