我試圖使用NSCache將PNG存儲爲NSData。每當我試圖讓一個背出緩存,緩存是否爲空或沒有,我得到:NSCache:嘗試訪問緩存項目時始終獲得「不符合鍵值編碼」的條件
2012年10月26日09:49:28.860 SledMap [55917:11503] *終止,由於應用程序未捕獲異常'NSUnknownKeyException',原因:'[valueForUndefinedKey:]:該類不是密鑰0_0_5的密鑰值編碼。「
如果我完全按照原樣保留代碼,但將NSCache更改爲NSMutableDictionary,則可以正常工作。
我宣佈我的緩存:
@property (nonatomic, strong) NSCache *tileCache;
分配它(在viewDidLoad中):
self.tileCache = [[NSCache alloc] init];
self.tileCache.delegate = self;
的東西添加到緩存:
NSString *key = [NSString stringWithFormat:@"%i_%i_%i",x,y,endLevel];
NSData *pngData = [NSData dataWithData:UIImagePNGRepresentation(image)];
[self.tileCache setObject:pngData forKey:key];
,然後當我拿回來出來,我得到了上述錯誤。
NSString *key = [NSString stringWithFormat:@"%i_%i_%i",x,y,endLevel];
NSData *tile = [self.tileCache valueForKey:key]; //This is the line it crashes on
如果是空的,我希望它只是返回nil,這是發生了什麼,當我做self.tileCache一個NSMutableDictionary,而不是一個NSCache。此外,在調試方面,它說:
tile = NSData * 0x0134dc59 <Variable is not NSData>
如果我通過一個NSString,它同樣和說變量沒有NSString的。
此外,我可以硬編碼key
爲「A」,並嘗試再次以「A」的形式訪問它,結果相同。
任何想法?
謝謝!就是這樣。 –