如何检索与FileManager的嵌套文件?

在我的应用程序中,我第一次启动应用程序时就创build了这个文件夹结构:

在这里输入图像说明

我在这里读取我可以保存数据库中的文件的绝对path吗? – 我不应该存储目录或文件的绝对path。 因此,我无法获得上图中显示的最后一个文件,称为 I-Want-This-File-Path

我可以访问上图中的Feed文件夹,如下所示:

 extension FileManager { /// APPLICATION SUPPORT DIRECTORY static func createOrFindApplicationSupportDirectory() -> URL? { let bundleID = Bundle.main.bundleIdentifier // Find the application support directory in the home directory. let appSupportDir = self.default.urls(for: .applicationSupportDirectory, in: .userDomainMask) guard appSupportDir.count > 0 else { return nil } // Append the bundle ID to the URL for the Application Support directory. let dirPath = appSupportDir[0].appendingPathComponent(bundleID!) // If the directory does not exist, this method creates it. do { try self.default.createDirectory(at: dirPath, withIntermediateDirectories: true, attributes: nil) return dirPath } catch let error { print("Error creating Application Support directory with error: \(error)") return nil } } /// FEED DIRECTORY static func createOrFindFeedDirectory() -> URL? { guard let appSupportDir = createOrFindApplicationSupportDirectory() else { return nil } let dirPath = appSupportDir.appendingPathComponent("Feed") // If the directory does not exist, this method creates it. do { try self.default.createDirectory(at: dirPath, withIntermediateDirectories: true, attributes: nil) return dirPath } catch let error { print("Error creating Favorites directory with error: \(error)") return nil } } } 

要访问FileStack文件夹,我有一个名为FileStack的对象,它保存了一部分path 。 我没有保存绝对path,而是在运行时像这样构build它:

 class FileStack { var title = "" var localDirectoryName: String? var files: File? // THIS OBJECT IS WHERE I WANT TO SAVE THE PATH // THIS IS HOW I BUILD THE URL AT RUNTIME TO GET THE PATH TO THE DIRECTORY CALLED "FILESTACK" var localDirectoryPath: URL? { guard let localDirectoryName = localDirectoryName else { return nil } return FileManager.createOrFindFeedDirectory()?.appendingPathComponent(localDirectoryName) } } 

注意属性var files: File? – 这是一个名为File的自定义对象,我想在上面的图片中保存I-Want-This-File-Path文件I-Want-This-File-Path 。 对象是这样的:

 class File { dynamic var iWantThisFileName: String? var iWantThisFilePath: URL? { guard let iWantThisFileName = iWantThisFileName else { return nil } return /// ??? HOW DO I RETURN THE PATH HERE? } } 

所以我最终希望能够 像这样 获得 I-Want-This-File-Path

 let fileStack = FileStack() fileStack.file.iWantThisFilePath // This will give me the path 

有什么build议么?

更新1

在应用程序支持 – > com.Company.DemoApp目录中,将会有多个文件夹。 例如,Feed和SavedForLater如下所示。 另外FileStack将有多个文件,如下所示。

在这里输入图像说明

更新2

换句话说,我在运行时无法构buildFile对象的path。

我需要将FileStacklocalDirectoryName给它的嵌套对象File因为File对象需要在运行时创build它的path。 我有上面的代码显示这样的事情。 如果这些是单独的对象,意思是没有嵌套在对方,我可以简单地传递一个url到下一个对象…但是因为它们是嵌套的,所以我被卡住了。


我认为你误解了其他问题的答案。 你想得到这个path:

 /Library/Application Support/com.Company.DemoApp/Feed/FileStack/I-Want-This-File 

/Library/Application Support/这是你不能在任何地方保存的部分,你必须在运行时从FileManager获取它。 这是因为你无法控制这条道路,如果没有你的批准,它可能会改变。 您希望应用程序中的path相对于此文件夹。

com.Company.DemoApp/Feed/FileStack/I-Want-This-File这是您可以存储在数据库/configuration文件中的文件path的一部分。 这个path只有在你改变的时候才会改变。 所以如果你更新你的应用程序并改变path,你也可以改变数据库中的path。

这是如何实施的粗略示例:

 enum DataDirectoryType : String { case wantedFile = "Feed/FileStack/I-Want-This-File-Path" case anotherFiles = "Feed/AnotherFiles/" } extension FileManager { /// APPLICATION SUPPORT DIRECTORY static private func createOrFindApplicationSupportDirectory() -> URL? { let bundleID = Bundle.main.bundleIdentifier // Find the application support directory in the home directory. let appSupportDir = self.default.urls(for: .applicationSupportDirectory, in: .userDomainMask) guard appSupportDir.count > 0 else { return nil } // Append the bundle ID to the URL for the Application Support directory. return appSupportDir[0].appendingPathComponent(bundleID!) } static func createOrFindFilePath(_ type : DataDirectoryType ) -> URL? { guard let appSupportDir = createOrFindApplicationSupportDirectory() else { return nil } let dirPath = appSupportDir.appendingPathComponent(type.rawValue) // If the directory does not exist, this method creates it. do { try self.default.createDirectory(at: dirPath, withIntermediateDirectories: true, attributes: nil) return dirPath } catch let error { print("Error creating Favorites directory with error: \(error)") return nil } } } let urlToWantedFile = FileManager.createOrFindFilePath(.wantedFile)