iOS - FileManager 扩展的最佳实践

标签 ios objective-c swift extension-methods nsfilemanager

我创建了这个 FileManager extension。有了这个扩展名,我想创建一个像这样的文件层次结构:

  • Application Support
    • Favorites
    • Feed
      • Images

这是我在 FileManager extension 中的代码,我会在应用程序启动后立即在 app delegate 中调用它。然后我将使用此代码始终检索文件夹的 path

这是创建此层次结构并在我需要时检索路径的好方法吗?这是好习惯吗?

extension FileManager {
    static func createOrFindApplicationDirectory() -> 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
        }
    }

    static func createOrFindFavoritesDirectory() -> URL? {
        guard let appSupportDir = createOrFindApplicationDirectory() else {
            return nil
        }

        let dirPath = appSupportDir.appendingPathComponent("Favorites")

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

    static func createOrFindFeedDirectory() -> URL? {
        guard let appSupportDir = createOrFindFavoritesDirectory() 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
        }
    }

    static func currentImagesDirectory() -> URL? {
        guard let feedDir = createOrFindFeedDirectory() else {
            return nil
        }

        let dirPath = feedDir.appendingPathComponent("Images")

        // 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 Images directory with error: \(error)")
            return nil
        }
    }
}

最佳答案

它看起来不错,但您可以结合一些代码并进行更好的错误检查:

extension FileManager {
    static func createOrFindApplicationDirectory() -> URL? {
        guard let bundleID = Bundle.main.bundleIdentifier else {
            return nil
        }

        // Find the application support directory in the home directory.
        let appSupportDirArray = self.default.urls(for: .applicationSupportDirectory, in: .userDomainMask)

        guard let appSupportDir = appSupportDirArray.first else {
            return nil
        }

        // Append the bundle ID to the URL for the Application Support directory.
        let dirPath = appSupportDir.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
        }
    }

    static func createOrFindDirectory(named name: String) -> URL? {
        guard let appSupportDir = createOrFindApplicationDirectory() else {
            return nil
        }

        let dirPath = appSupportDir.appendingPathComponent(name)

        // 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 \(name) directory with error: \(error)")
            return nil
        }
    }

    static func currentImagesDirectory() -> URL? {
        guard let feedDir = createOrFindDirectory(named: "Feed") else {
            return nil
        }

        let dirPath = feedDir.appendingPathComponent("Images")

        // 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 Images directory with error: \(error)")
            return nil
        }
    }
}

关于iOS - FileManager 扩展的最佳实践,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41643818/

相关文章:

objective-c - UITableView 字幕没有出现

iphone - UIButton setImage 不起作用?

ios - 当应用程序处于后台并且网络连接丢失时,位置更新计时器不起作用?

ios - UITextField 的占位符边距和初始光标边距

iphone - UITableViewCell textLabel 宽度不变

objective-c - 如何在 iOS 上的计时器内更新 UI?

ios - 使用 UIWebView 快速滚动

ios - Touch ID API 响应速度很慢

ios - NSUserDefaults.standardUserDefaults().stringForKey ("useremail") 值返回 null

swift - 如何使用 committedValuesForKeys 保存和重构 NSManagedObjects?