How to use NSCache

后端 未结 5 1928
Happy的楠姐
Happy的楠姐 2020-11-27 10:25

Can someone give an example on how to use NSCache to cache a string? Or anyone has a link to a good explanation? I can\'t seem to find any..

5条回答
  •  南方客
    南方客 (楼主)
    2020-11-27 10:58

    Shouldn't the cached objects implement the NSDiscardableContent protocol?

    From the NSCache class reference: A common data type stored in NSCache objects is an object that implements the NSDiscardableContent protocol. Storing this type of object in a cache has benefits, because its content can be discarded when it is not needed anymore, thus saving memory. By default, NSDiscardableContent objects in the cache are automatically removed from the cache if their content is discarded, although this automatic removal policy can be changed. If an NSDiscardableContent object is put into the cache, the cache calls discardContentIfPossible on it upon its removal.

提交回复
热议问题