Instance Method インスタンスメソッド

setObject(_:forKey:cost:)

Sets the value of the specified key in the cache, and associates the key-value pair with the specified cost. キャッシュの中の指定されたキーの値を設定します、そしてそのキー値ペアを指定されたコストと結びつけます。

Declaration 宣言

func setObject(_ obj: ObjectType, 
        forKey key: KeyType, 
          cost g: Int)

Parameters パラメータ

obj

The object to store in the cache. キャッシュの中に格納するオブジェクト。

key

The key with which to associate the value. それと値を結びつけることになるキー。

g

The cost with which to associate the key-value pair. それとこのキー値ペアを結び付けることになるコスト。

Discussion 議論

The cost value is used to compute a sum encompassing the costs of all the objects in the cache. When memory is limited or when the total cost of the cache eclipses the maximum allowed total cost, the cache could begin an eviction process to remove some of its elements. However, this eviction process is not in a guaranteed order. As a consequence, if you try to manipulate the cost values to achieve some specific behavior, the consequences could be detrimental to your program. Typically, the obvious cost is the size of the value in bytes. If that information is not readily available, you should not go through the trouble of trying to compute it, as doing so will drive up the cost of using the cache. Pass in 0 for the cost value if you otherwise have nothing useful to pass, or simply use the setObject:forKey: method, which does not require a cost value to be passed in. cost値は、キャッシュの中のオブジェクト全てのコストを網羅する合計を計算するために使われます。メモリが制限される場合またはキャッシュのトータルコストが最大限許されるトータルコストを越える場合、キャッシュは立ち退き処理を開始してその要素のいくつかを除去できます。しかしながら、この除去処理は保証された順番ではありません。それゆえに、あなたがコスト値を操っていくつかの特定の挙動を獲得しようとするならば、結果はあなたのプログラムに有害でありえるでしょう。概して、明らかなコストはバイトでの値のサイズです。その情報が容易に利用可能ではないならば、あなたはそれを計算しようとする手間をかけるべきではありません、そうすることでキャッシュ使用のコストがつり上がるでしょう。あなたがそれ以外に渡すのに役立つものを持たないならば、コスト値に対して0を渡してください、または単にsetObject:forKey:メソッドを使ってください、それはcost値を渡されることを必要としません。

Unlike an NSMutableDictionary object, a cache does not copy the key objects that are put into it. NSMutableDictionaryオブジェクトと違い、キャッシュはそれの中へと置かれるキーオブジェクトをコピーしません。

See Also 参照

Adding and Removing Cached Values キャッシュされた値を追加および除去する

Related Documentation 関連文書