如何使用NSCache

有人可以举一个例子如何使用NSCachecaching一个string? 或者任何人有一个很好的解释的链接? 我似乎无法find任何..

你可以像使用NSMutableDictionary一样使用它。 不同的是,当NSCache检测到内存压力过大(即caching了太多的值)时,它会释放一些值来腾出空间。

如果您可以在运行时重新创build这些值(通过从互联网上下载,通过计算,无论什么),那么NSCache可能会满足您的需求。 如果数据不能被重新创build(例如,它是用户input,时间敏感等),那么你不应该把它存储在NSCache因为它会在那里被销毁。

例如,不考虑线程安全性:

 // Your cache should have a lifetime beyond the method or handful of methods // that use it. For example, you could make it a field of your application // delegate, or of your view controller, or something like that. Up to you. NSCache *myCache = ...; NSAssert(myCache != nil, @"cache object is missing"); // Try to get the existing object out of the cache, if it's there. Widget *myWidget = [myCache objectForKey: @"Important Widget"]; if (!myWidget) { // It's not in the cache yet, or has been removed. We have to // create it. Presumably, creation is an expensive operation, // which is why we cache the results. If creation is cheap, we // probably don't need to bother caching it. That's a design // decision you'll have to make yourself. myWidget = [[[Widget alloc] initExpensively] autorelease]; // Put it in the cache. It will stay there as long as the OS // has room for it. It may be removed at any time, however, // at which point we'll have to create it again on next use. [myCache setObject: myWidget forKey: @"Important Widget"]; } // myWidget should exist now either way. Use it here. if (myWidget) { [myWidget runOrWhatever]; } 
 @implementation ViewController { NSCache *imagesCache; } - (void)viewDidLoad { imagesCache = [[NSCache alloc] init]; } // How to save and retrieve NSData into NSCache NSData *imageData = [imagesCache objectForKey:@"KEY"]; [imagesCache setObject:imageData forKey:@"KEY"]; 

在Swift中使用NSCachecachingstring的示例代码:

 var cache = NSCache() cache.setObject("String for key 1", forKey: "Key1") var result = cache.objectForKey("Key1") as String println(result) // Prints "String for key 1" 

要创build单个应用程序范围的NSCache实例(单例),可以轻松扩展NSCache以添加sharedInstance属性。 只需将下面的代码放在一个名为NSCache + Singleton.swift的文件中:

 import Foundation extension NSCache { class var sharedInstance : NSCache { struct Static { static let instance : NSCache = NSCache() } return Static.instance } } 

然后,您可以在应用程序的任何位置使用caching:

 NSCache.sharedInstance.setObject("String for key 2", forKey: "Key2") var result2 = NSCache.sharedInstance.objectForKey("Key2") as String println(result2) // Prints "String for key 2" 

示例项目将示例项目中的 CacheController.h和.m文件添加到项目中。 在你想要caching数据的类中,input下面的代码。

 [[CacheController storeInstance] setCache:@"object" forKey:@"objectforkey" ]; 

你可以使用这个设置任何对象

 [[CacheController storeInstance] getCacheForKey:@"objectforkey" ]; 

回顾

重要:NSCache类包含各种自动删除策略。 如果你想caching永久数据或者你想在特定的时间删除caching的数据看到这个答案 。

不应该caching的对象实现NSDiscardableContent协议?

从NSCache类引用:存储在NSCache对象中的常见数据types是实现NSDiscardableContent协议的对象。 将这种types的对象存储在caching中是有好处的,因为它的内容可以在不再需要时被丢弃,从而节省内存。 默认情况下,如果caching中的NSDiscardableContent对象的内容被丢弃,它们将自动从caching中删除,尽pipe可以更改此自动删除策略。 如果一个NSDiscardableContent对象被放入caching中,caching在移除时调用discardContentIfPossible。