贡献者:赵大财
博客:https://my.oschina.net/zhaodacaiGitHub:https://github.com/dacaizhao
邮箱: dacai_zhao@163.com QQ:327532817
=============================
class ViewController: UIViewController,NSCacheDelegate { override func viewDidLoad() { super.viewDidLoad() let cache = NSCache<AnyObject,AnyObject>() cache.totalCostLimit = 5 cache.delegate = self cache.setObject("hahah" as AnyObject,forKey: "1" as AnyObject,cost: 1) print(cache.object(forKey: "1" as AnyObject) ?? "") cache.removeObject(forKey: "1" as AnyObject) } func cache(_ cache: NSCache<AnyObject,AnyObject>,willEvictObject obj: Any) { print("完了") } }原文链接:https://www.f2er.com/swift/322502.html