iOS - FileManager 扩展的最佳实践
Posted
技术标签:
【中文标题】iOS - FileManager 扩展的最佳实践【英文标题】:iOS - Best practices for FileManager extensions 【发布时间】:2017-01-13 21:37:12 【问题描述】:我创建了这个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
【问题讨论】:
我觉得不错。 @ILikeTau 谢谢!你有什么不同的做法吗?我希望看到其他一些示例,无论是您自己的代码还是与我正在尝试做的事情类似的教程/链接。 我可能会将createOrFindFavoritesDirectory()
和createOrFindFeedDirectory()
组合成一个带参数的函数,但除此之外,它看起来都很好。
@ILikeTau 你能发布一个答案吗?我只是很想知道其他人会如何做到这一点——尝试向比我更好的人学习:)
【参考方案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
【讨论】:
看起来不错!我看到您将 Feed 和收藏夹合并为一个功能static func createOrFindDirectory(named name: String) -> URL?
,很酷。就“错误检查”而言,我确实需要在首次启动应用程序时创建这些文件夹/目录。除了返回nil
之外,您如何处理错误,以便我保证这些文件夹是在继续之前创建的?关键是,如果没有在应用启动时创建这些文件夹,我的应用将无法正常运行
既然你提到了错误处理,这让我想了更多。我不确定“创建”这些文件夹之一可能会失败的确切时间,但如果确实如此,不知道如何处理它这样应用在创建之前不会继续进行以上是关于iOS - FileManager 扩展的最佳实践的主要内容,如果未能解决你的问题,请参考以下文章