简体   繁体   English

NSCache:尝试访问缓存项时始终获得“不符合键值编码”

[英]NSCache: Always getting “not key value coding-compliant” when trying to access cached item

I'm attempting to use NSCache to store PNGs as NSData. 我正在尝试使用NSCache将PNG存储为NSData。 Whenever I try to get one back out of the cache, whether the cache is empty or not, I get: 每当我尝试从缓存中取出一个缓存,无论缓存是否为空,我得到:

2012-10-26 09:49:28.860 SledMap[55917:11503] * Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ valueForUndefinedKey:]: this class is not key value coding-compliant for the key 0_0_5.' 2012-10-26 09:49:28.860 SledMap [55917:11503] *由于未捕获的异常'NSUnknownKeyException'终止应用程序,原因:'[valueForUndefinedKey:]:此类不是密钥值0_0_5的编码兼容。

If I leave the code exactly as is, but change NSCache to NSMutableDictionary, it works fine. 如果我完全保留代码,但将NSCache更改为NSMutableDictionary,它可以正常工作。

I declare my cache: 我宣布我的缓存:

@property (nonatomic, strong) NSCache *tileCache;

Allocate it (in viewDidLoad): 分配它(在viewDidLoad中):

self.tileCache = [[NSCache alloc] init];
self.tileCache.delegate = self;

Add something to the cache: 添加内容到缓存:

NSString *key = [NSString stringWithFormat:@"%i_%i_%i",x,y,endLevel];
NSData *pngData = [NSData dataWithData:UIImagePNGRepresentation(image)];
[self.tileCache setObject:pngData forKey:key];

And then when I get it back out, I get the above error. 然后当我把它拿回来时,我得到了上述错误。

NSString *key = [NSString stringWithFormat:@"%i_%i_%i",x,y,endLevel];
NSData *tile = [self.tileCache valueForKey:key];  //This is the line it crashes on

If it was empty, I would expect it to just return nil, which is what happens when I make self.tileCache an NSMutableDictionary instead of an NSCache. 如果它是空的,我希望它只返回nil,这就是当我使self.tileCache成为NSMutableDictionary而不是NSCache时会发生的事情。 Also, in the debug area, it says: 此外,在调试区域,它说:

tile = NSData * 0x0134dc59 <Variable is not NSData>

If I pass in an NSString, it does the same and says variable is not NSString. 如果我传入NSString,它会做同样的事情,并且变量不是NSString。

Also, I can hard code key to be "A" and try to access it again as "A" with the same results. 此外,我可以将key硬编码为“A”并尝试再次访问它作为“A”具有相同的结果。

Any ideas? 有任何想法吗?

-valueForKey: is a KVC method; -valueForKey:是一个KVC方法; use -objectForKey: 使用-objectForKey:

I don't think that the issue is the NSData, I think it might be with your key. 我不认为问题是NSData,我认为它可能与您的密钥有关。 Take a close look at that stringWithFormat - you should probably be using %d instead of %i if they are both integers, because %d is 32bit compliant. 仔细看看stringWithFormat - 如果它们都是整数,你应该使用%d而不是%i,因为%d符合32位。 If they are floats you should be using %f. 如果它们是花车,你应该使用%f。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 NSManagedObject密钥存在于实体描述中,但是当尝试访问它时,应用程序崩溃,并显示“此类不符合键值编码标准” - NSManagedObject key exists in the entity description but when trying to access it the app crashes with “this class is not key value coding-compliant” 尝试使用custon UITableViewCell时发生错误“此类不符合键的键值编码标准……” - Error when trying to use custon UITableViewCell “this class is not key value coding-compliant for the key…” 使用故事板而不是笔尖时,如何解决此问题?:获取“此类不符合键值编码”? - How can I fix this when using a Storyboard, instead of a nib?: Getting “this class is not key value coding-compliant"? “这个类不是密钥值编码兼容的密钥” - “this class is not key value coding-compliant for the key” iOS - 使用关联时“此类不符合键值编码”? - iOS - “This class is not key value coding-compliant” when using associations? 实体不符合密钥的密钥值编码要求 - Entity is not key value coding-compliant for the key 此类不符合key的键值编码要求。 - this class is not key value coding-compliant for the key .' “此类不符合密钥的键值编码标准……” - “this class is not key value coding-compliant for the key…” NSObject类的键值编码兼容? - key value coding-compliant for NSObject class? 此类不符合键值编码 - This class is not key value coding-compliant
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM