小编Pau*_*olt的帖子

NSCache不会驱逐数据

NSCache是一种很少使用的工具,实际上看起来非常有用.我创建了一个简单的实验,看看它是如何工作的,看起来它在低内存情况下不会自动驱逐数据(或者我做错了!)

- (void)viewDidLoad
{
    _testCache = [[NSCache alloc] init];

    // Allocate 600 MB of zeros and save to NSCache
    NSMutableData* largeData = [[NSMutableData alloc] init];
    [largeData setLength:1024 * 1024 * 600]; 
    [_testCache setObject:largeData forKey:@"original_Data"];
}

- (IBAction)buttonWasTapped:(id)sender {

    // Allocate & save to cache 300 MB each time the button is pressed
    NSMutableData* largeData = [[NSMutableData alloc] init];
    [largeData setLength:1024 * 1024 * 300]; 
    static int count = 2;
    NSString* key = [NSString stringWithFormat:@"test_data_%d", count++];
    [_testCache setObject:largeData forKey:key];

    NSMutableData* …
Run Code Online (Sandbox Code Playgroud)

iphone objective-c ios nscache

11
推荐指数
2
解决办法
8024
查看次数

标签 统计

ios ×1

iphone ×1

nscache ×1

objective-c ×1