Tip: date: 2018.10.01, Develop With Swift
Get documents directory and file path(获取沙盒和具体文件路径)
1. SWDocumentsManager 管理路径的方法
import UIKit
class SWDocumentsManager: NSObject {
static let shared = SWDocumentsManager() /// 管理路径的管家
/// 获取沙盒路径
func documentDirectory() -> URL {
let urls = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
return urls[0]
}
/// 获取具体文件路径
func fileUrl(_ fileName: String) -> URL {
let documentUrl = self.documentDirectory().appendingPathComponent(fileName)
return documentUrl
}
}
2.通过单例调用获取路径的方法
func path() {
/// 获取沙盒路径
let doctmentsDirUrl = SWDocumentsManager.shared.documentDirectory()
/// 获取具体文件路径
let plistUrl = SWDocumentsManager.shared.fileUrl("EpisodeList.plist")
print("Doctments Dir Url: ===== \(doctmentsDirUrl)")
print("Plist File Url: ===== \(plistUrl)")
}
3. 效果图
网友评论