我不知道我在这里缺少什么来缓存对象。但是没有任何对象被缓存在磁盘中
import UIKit
class MyObject {
var title:String?
var desc:String?
init(title: String, desc: String) {
self.title = title
self.desc = desc
}
}
class ViewController: UIViewController {
let cache = NSCache<NSString, MyObject>()
var myObject:MyObject?
override func viewDidLoad() {
super.viewDidLoad()
if let cachedVersion = cache.object(forKey: "CachedObject") {
// use the cached version
myObject = cachedVersion
} else {
// create it from scratch then store in cache
let object = MyObject(title: "title", desc: "desc")
cache.setObject(object, forKey: "CachedObject")
}
}
}
第二次运行Xcode项目后,我希望看到键“CachedObject”的值