/*
文件路径工具类
*/
import UIKit
class FilePathTool: NSObject {
// MARK: - 单例
static let share:FilePathTool = {
let instance = FilePathTool()
return instance
}()
private override init() {
}
// MARK: - 系统文件路径
let documentPath:String = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)[0]
let libraryPath:String = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.libraryDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)[0]
let cachePath:String = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.cachesDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)[0]
let tempPath = NSTemporaryDirectory()
// MARK: - 自定义文件路径
lazy var loginResultPath: String = {//登录结果数据库路径
let path = self.documentPath + "/loginResultDB.realm"
return path
}()
}
// MARK: - 文件路径创建相关
extension FilePathTool {
func createPath(path: String) {//创建路径(如果存在不创建,如果不存在则创建)
if FileManager.default.fileExists(atPath: path) {
return
}
try! FileManager.default.createDirectory(atPath: path, withIntermediateDirectories: true, attributes: nil)
}
func fileExists(path: String?) -> Bool {//判断路径是否存在
if let filePath = path {
return FileManager.default.fileExists(atPath: filePath)
}
return false
}
}
网友评论