gpt4 book ai didi

ios - 如何使用 FileManager 检索嵌套文件?

转载 作者:行者123 更新时间:2023-11-30 12:45:51 26 4
gpt4 key购买 nike

在我的应用程序中,我会在应用程序首次启动时创建此文件夹结构:

enter image description here

我在这里读到Can I save the keep the absolute path of a file in database? - 我不应该存储目录或文件的绝对路径。 因此,我无法引用上图中显示的最后一个名为 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的对象,它保存了路径的一部分。我没有保存绝对路径,而是在运行时构建它,如下所示:

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文件。该对象如下所示:

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

有什么建议吗?

更新 1

在Application Support -> com.Company.DemoApp目录下,会有多个文件夹。例如,Feed 和 SavedForLater 如下所示。另外,FileStack 将有多个文件,如此处所示。

enter image description here

更新2

换句话说,我在运行时构建 File 对象的路径时遇到了困难。

我需要将FileStacklocalDirectoryName传递给它的嵌套对象File,因为File对象将需要它在运行时构建它的路径。我上面的代码显示了类似这样的内容。如果这些是单独的对象,意味着不相互嵌套,我可以简单地将 url 传递给下一个对象......但由于它们是嵌套的,我陷入困境。

<小时/>

最佳答案

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

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

/Library/Application Support/ 这是您无法保存在任何地方的部分,您必须在运行时从 FileManager 获取它。这是因为您无法控制此路径,并且可以在未经您批准的情况下进行更改。您希望应用程序中的路径相对于此文件夹。

com.Company.DemoApp/Feed/FileStack/I-Want-This-File 这是您可以存储在数据库/配置文件中的文件路径的一部分。仅当您更改该路径时,该路径才会更改。因此,如果您更新应用程序并更改路径,您还可以更改数据库中的路径。

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

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)

关于ios - 如何使用 FileManager 检索嵌套文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41655887/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com