Swift中的NSCache:不保存到磁盘

时间:2018-05-27 18:33:31

标签: swift caching nscache

我不知道我在这里缺少什么来缓存对象。但是没有任何对象被缓存在磁盘中

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”的值

enter image description here

0 个答案:

没有答案