誰かNSCacheが文字列をキャッシュする方法の例を挙げられますか?または誰かが良い説明へのリンクを持っていますか?見つからないようです。
誰かNSCacheが文字列をキャッシュする方法の例を挙げられますか?または誰かが良い説明へのリンクを持っていますか?見つからないようです。
回答:
使用方法と同じように使用しますNSMutableDictionary。違いは、NSCache過度のメモリプレッシャーを検出した場合(つまり、キャッシュする値が多すぎる場合)、それらの値の一部を解放して領域を空けることです。
実行時にこれらの値を(インターネットからダウンロードしたり、計算を行ったりして)再作成できる場合は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];
}
applicationDidEnterBackground)
                    NSCacheオブジェクトの割り当てが解除されない場合、はい、それはメモリに残ります。ただし、その内容はカリングの対象になる場合があります。
                    @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で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"class Cache: NSCache<AnyObject, AnyObject> {     static let shared = NSCache<AnyObject, AnyObject>()     private override init() { super.init() } }
                    サンプルプロジェクトサンプルプロジェクトからプロジェクト にCacheController.hおよび.mファイルを追加します。データをキャッシュしたいクラスに、以下のコードを記述します。
[[CacheController storeInstance] setCache:@"object" forKey:@"objectforkey" ];これを使用して任意のオブジェクトを設定できます
[[CacheController storeInstance] getCacheForKey:@"objectforkey" ];取得する
重要:NSCacheクラスには、さまざまな自動削除ポリシーが組み込まれています。永続的にデータをキャッシュする場合、または特定の時間にキャッシュデータを削除する場合は、 この回答を参照してください。
キャッシュされたオブジェクトはNSDiscardableContentプロトコルを実装すべきではありませんか?
NSCacheクラスリファレンスから:NSCacheオブジェクトに格納される一般的なデータ型は、NSDiscardableContentプロトコルを実装するオブジェクトです。このタイプのオブジェクトをキャッシュに格納すると、コンテンツが不要になったときに破棄できるため、メモリを節約できるという利点があります。デフォルトでは、キャッシュ内のNSDiscardableContentオブジェクトは、コンテンツが破棄されると自動的にキャッシュから削除されますが、この自動削除ポリシーは変更できます。NSDiscardableContentオブジェクトがキャッシュに入れられると、キャッシュは削除時にdiscardContentIfPossibleを呼び出します。