iOS - FileManager扩展的最佳做法

时间:2017-01-13 21:37:13

标签: ios objective-c swift extension-methods nsfilemanager

我创建了这个FileManager extension。使用此extension,我想创建一个文件层次结构,如下所示:

  
      
  • 应用程序支持      
        
    • 收藏夹
    •   
    • 饲料      
          
      • 图片
      •   
    •   
  •   

这是我在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
        }
    }
}

1 个答案:

答案 0 :(得分:1)

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

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