Ios 如何使用NSCache
有人能举例说明如何使用Ios 如何使用NSCache,ios,objective-c,xcode,cocoa,nscache,Ios,Objective C,Xcode,Cocoa,Nscache,有人能举例说明如何使用NSCache缓存字符串吗? 或者有人能找到一个好的解释?我似乎找不到任何..您使用它的方式与使用NSMutableDictionary的方式相同。不同之处在于,当NSCache检测到内存压力过大(即缓存过多的值)时,它会释放其中一些值以腾出空间 如果您可以在运行时重新创建这些值(通过从Internet下载、进行计算等等),那么NSCache可能适合您的需要。如果无法重新创建数据(例如,数据的用户输入、时间敏感等),则不应将其存储在NSCache中,因为它将在那里被销毁 例
NSCache
缓存字符串吗?
或者有人能找到一个好的解释?我似乎找不到任何..您使用它的方式与使用
NSMutableDictionary
的方式相同。不同之处在于,当NSCache
检测到内存压力过大(即缓存过多的值)时,它会释放其中一些值以腾出空间
如果您可以在运行时重新创建这些值(通过从Internet下载、进行计算等等),那么NSCache
可能适合您的需要。如果无法重新创建数据(例如,数据的用户输入、时间敏感等),则不应将其存储在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];
}
将CacheController.h和.m文件从示例项目添加到您的项目中。在要缓存数据的类中,放置以下代码
[[CacheController storeInstance] setCache:@"object" forKey:@"objectforkey" ];
可以使用此选项设置任何对象
[[CacheController storeInstance] getCacheForKey:@"objectforkey" ];
重审
重要提示:NSCache类包含各种自动删除策略。如果要缓存永久数据或要在特定时间内删除缓存数据。在Swift中使用NSCache缓存字符串的示例代码:
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"
要创建NSCache的单个应用程序范围实例(单实例),可以轻松扩展NSCache以添加sharedInstance属性。只需将以下代码放入名为NSCache+Singleton.swift的文件中:
import Foundation
extension NSCache {
class var sharedInstance : NSCache {
struct Static {
static let instance : NSCache = NSCache()
}
return Static.instance
}
}
然后,您可以在应用程序中的任意位置使用缓存:
NSCache.sharedInstance.setObject("String for key 2", forKey: "Key2")
var result2 = NSCache.sharedInstance.objectForKey("Key2") as String
println(result2) // Prints "String for key 2"
缓存对象不应该实现NSDiscardableContent协议吗 从NSCache类参考中:
NSCache对象中存储的公共数据类型是实现NSDiscardableContent协议的对象。将这种类型的对象存储在缓存中有好处,因为当不再需要它时,可以丢弃它的内容,从而节省内存。默认情况下,如果缓存中的NSDiscardableContent对象的内容被丢弃,则会自动从缓存中删除这些对象,尽管可以更改此自动删除策略。如果NSDiscardableContent对象被放入缓存中,缓存会在其删除时调用DiscardableContentIfEnabled。尽管看到大量可可豆,但我从未听说过NSCache。好问题!我在这里创建了一个可移植版本的NSCache。请随意参与:如何实例化NSCache对象?每次运行应用程序时,我都会丢失所有缓存的信息。我用;[[NSCache alloc]init]它不是磁盘上的永久缓存。它只在内存中,所以是的,每次应用程序停止运行时,它都会被销毁。在应用程序进入Background后,这个缓存是否会被维护?(即在
applicationidentinterbackground
之后)如果应用程序未终止且NSCache
对象未解除分配,则它将保留在内存中。其内容可能仍会被剔除,但不会。断言断言某物为真,即其中的语句应为真。我们断言对象分配和/或创建成功。是。保存:[imagesDictionary setObject:imageData forKey:@“KEY”];检索:NSData*imageData=[imagesCache objectForKey:@“KEY”];将数据保存到chache:[imagesCache setObject:imageData forKey:@“KEY”];我刚刚为imagesCache编辑了一篇文章:imagesDictionary。ThxAn等效swift示例?您可以实现NSDiscardableContent协议,但这不是必需的。当没有更多引用时,NSDiscardableContent似乎总是从NSCache中删除。NSDiscardableContent对象以外的对象存储在NSCache中,直到出现“内存紧张”且NSCache将被清除。您知道如何在swift 3中实现这一点吗?这应该可以工作:类缓存:NSCache{static let shared=NSCache()private override init(){super.init()}
NSCache.sharedInstance.setObject("String for key 2", forKey: "Key2")
var result2 = NSCache.sharedInstance.objectForKey("Key2") as String
println(result2) // Prints "String for key 2"