class OSSKeyValueCacheHelper: NSObject {
static let shared = OSSKeyValueCacheHelper()
private let fileManager = FileManager.default
private let cachePath = (NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.cachesDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).first ?? "") + "/OSSKeyValueCacheDictionary.plist"
private lazy var tempUrlCacheDict = Dictionary<String, String>()
override init() {
super.init()
if fileManager.fileExists(atPath: cachePath) {
if let dict = loadDictionary() {
tempUrlCacheDict = dict
}
} else {
fileManager.createFile(atPath: cachePath, contents: try? PropertyListSerialization.data(fromPropertyList: Dictionary<String, String>(), format: PropertyListSerialization.PropertyListFormat.binary, options: 0))
}
}
private func getUrlPath() -> URL {
if #available(iOS 16.0, *) {
return URL(filePath: cachePath)
} else {
return URL(fileURLWithPath: cachePath)
}
}
func clearTempUrlCacheDict() {
tempUrlCacheDict.removeAll()
saveDictionary(dict: tempUrlCacheDict)
}
func getTempUrlCacheDict() -> Dictionary<String, String> {
return tempUrlCacheDict
}
func saveDictionary(dict: Dictionary<String, String>) {
try? NSDictionary(dictionary: dict).write(to: getUrlPath())
}
func loadDictionary() -> Dictionary<String, String>? {
if let dict = try? NSDictionary(contentsOf: getUrlPath(), error: ()) as? Dictionary<String, String> {
return dict
}
return nil
}
}
Swift 保存字典plist到沙盒
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 闲话少说,先上Demo Github地址:https://github.com/Jerryisme/Diction...
- 前言: 本篇接着上一篇iOS Swift 原生 字典数组转模型 JSONDecoder 对象存储 NSKeyedA...
- 最近swift大热,为了追随潮流,最近一直在学习swift(好吧,其实是怕饭碗不保~~)。 言归正传,这遍blog...