Skip to content

Commit

Permalink
Path - 更新路径相关函数
Browse files Browse the repository at this point in the history
  • Loading branch information
Harley-xk committed Jan 7, 2020
1 parent 1d9edb6 commit 8b4dac9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 34 deletions.
4 changes: 2 additions & 2 deletions Comet.podspec.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "Comet",
"version": "1.6.2",
"version": "1.6.3",
"summary": "iOS 项目的 Swift 基础库,提供一些常用组件、便利方法等。",
"description": "iOS 项目的 Swift 基础库,提供一些常用组件、便利方法等。1.0 起支持 Swift 4+",
"homepage": "https://github.com/Harley-xk/Comet",
Expand All @@ -13,7 +13,7 @@
},
"source": {
"git": "https://github.com/Harley-xk/Comet.git",
"tag": "1.7.0"
"tag": "1.6.3"
},
"platforms": {
"ios": "8.0"
Expand Down
55 changes: 23 additions & 32 deletions Comet/Classes/Path.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,32 +23,36 @@ open class Path {
url = path
}

init(directory: FileManager.SearchPathDirectory, create: Bool = false) throws {
url = try FileManager.default.url(for: directory, in: .userDomainMask, appropriateFor: nil, create: create)
}

/// 完整路径字符串
open var string: String {
return url.relativePath
return url.relativeString
}

/// URL 实例
open var url: URL

/// 获取沙盒 Documents 路径
/// 获取沙盒 Documents
open class func documents() -> Path {
return NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0].path
return try! Path(directory: .documentDirectory)
}

/// 获取沙盒 Library 路径
open class func library() -> Path {
return NSSearchPathForDirectoriesInDomains(.libraryDirectory, .userDomainMask, true)[0].path
return try! Path(directory: .libraryDirectory)
}

/// 获取沙盒 Cache 路径
open class func cache() -> Path {
return NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true)[0].path
return try! Path(directory: .cachesDirectory)
}

/// 获取沙盒 Temp 路径
open class func temp() -> Path {
return NSTemporaryDirectory().path
return Path(NSTemporaryDirectory())
}

/// 获取沙盒 Application Support 路径,不存在时会自动创建
Expand All @@ -71,8 +75,16 @@ open class Path {
///
/// - Parameter name: 资源文件名(含扩展名)
open func resource(_ name: String) -> Path {
let directory = string as NSString
return directory.appendingPathComponent(name).path
let res = url.appendingPathComponent(name, isDirectory: false)
return Path(res)
}

/// 获取当前目录下的子目录
///
/// - Parameter name: 资源文件名(含扩展名)
open func folder(_ name: String) -> Path {
let res = url.appendingPathComponent(name, isDirectory: true)
return Path(res)
}

// MARK: - Path Utils
Expand All @@ -95,29 +107,19 @@ open class Path {

/// 文件夹是否存在, 返回是否存在,(以及必须是文件夹)
open var folderExist: Bool {
return !fileExist.isFile
return fileExist.exist && !fileExist.isFile
}

/// 文件扩展名
open var pathExtension: String? {
let path = string as NSString
return path.pathExtension
return url.pathExtension
}

/// 创建路径
/// - overrides: 是否覆盖已经存在的文件夹,默认 false
open func createDirectory(overrides: Bool = false) throws {
if overrides && folderExist {
try? removeFromDisk()
}
open func createDirectory() throws {
try fileManager.createDirectory(at: url, withIntermediateDirectories: true, attributes: nil)
}

/// 删除当前路径指向的物理文件(夹)
open func removeFromDisk() throws {
try fileManager.removeItem(at: url)
}

/// 获取文件夹子目录
open func getChildren(skipsHiddenFiles: Bool = true) throws -> [Path] {
guard folderExist else {
Expand Down Expand Up @@ -201,11 +203,6 @@ open class Path {
}
}

// MARK: - Datas
open func readData(options: Data.ReadingOptions = []) throws -> Data {
return try Data(contentsOf: url, options: options)
}

// MARK: - Private
internal func fileSize() -> UInt64 {
let fileExist = self.fileExist
Expand Down Expand Up @@ -257,9 +254,3 @@ public extension Bundle {
return string == nil ? nil : Path(string!)
}
}

public extension String {
var path: Path {
return Path(self)
}
}

0 comments on commit 8b4dac9

Please sign in to comment.