0

NSCache を拡張し、以下のように Singleton クラスを作成しました

+(instancetype)sharedDataCache {
if (!sharedDataCache) {
    sharedDataCache = [[QDataCache alloc] init];
    [sharedDataCache setTotalCostLimit:19999];
    [sharedDataCache setCountLimit:15];
    [[NSNotificationCenter defaultCenter] addObserver:sharedDataCache selector:@selector(removeAllObjects) name:UIApplicationDidReceiveMemoryWarningNotification object:nil];
}
return sharedDataCache;
}

1 つのクラスでのオブジェクトの設定:

[_cache setObject:receivedData forKey:[[connection currentRequest]URL]];

および他のクラスのオブジェクトの取得:

image = [UIImage imageWithData:[_caches objectForKey:keyString]];//keystring is the url

両方のキーはログで同じですが、NULL を取得しています :( .「IF I HARDCODE THE KEY WITH SAME STRING IT IS WORKING FINE」.

4

1 に答える 1

1

NSURL キーで値を入力してから、NSString キーで値を取得することはできません。NSURL オブジェクトと NSString オブジェクトのハッシュは同じではありません。

どちらかを行う必要があります

[_cache setObject:receivedData forKey:[[[connection currentRequest]URL] absoluteString]];

または

image = [UIImage imageWithData:[_caches objectForKey:[NSURL URLWithString:keyString]]];
于 2015-05-13T11:33:45.557 に答える