实现nscache时出错

时间:2013-08-20 11:58:05

标签: ios nscache

我有这个错误

    2013-08-20 17:06:57.500 AceXMLSample[13602:11303] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<NSCache 0x753a5d0> valueForUndefinedKey:]: this class is not key value coding-compliant for the key http://skylitecommunication.ipower.com/--irabwha-opc/image/data/1287455832_monotone_fork_spoon_eat_launch_restaurant_dinner.png.'
*** First throw call stack:
(0x1c95012 0x10d2e7e 0x1d1dfb1 0xb7ed1d 0xaeb00b 0xaeafbd 0x2c22 0xd18fb 0xd19cf 0xba1bb 0xcab4b 0x672dd 0x10e66b0 0x2291fc0 0x228633c 0x2291eaf 0x1062bd 0x4eb56 0x4d66f 0x4d589 0x4c7e4 0x4c61e 0x4d3d9 0x502d2 0xfa99c 0x47574 0x4776f 0x47905 0x50917 0x2465 0x14157 0x14747 0x1594b 0x26cb5 0x27beb 0x19698 0x1bf0df9 0x1bf0ad0 0x1c0abf5 0x1c0a962 0x1c3bbb6 0x1c3af44 0x1c3ae1b 0x1517a 0x16ffc 0x219d 0x20c5)
libc++abi.dylib: terminate called throwing an exception
(lldb) 

这是我有这种错误的代码。不知道是什么问题。

NSString *imageURL = currentData.imageLink;    
NSData *imageData = [myCache valueForKey:imageURL];
if (imageData != nil) {
    [cell.imageShow setImage:[UIImage imageWithData:imageData]];
} else {
    NSData *dataToCache = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageURL]];
    [myCache setObject:dataToCache forKey:imageURL];
    [cell.imageShow setImage:[UIImage imageWithData:dataToCache]];
}

1 个答案:

答案 0 :(得分:4)

你没有给出完整的错误,但我想你需要替换:

NSData *imageData = [myCache valueForKey:imageURL];

NSData *imageData = [myCache objectForKey:imageURL];

您可能需要添加一些保护,例如:

if (imageURL != nil) {
    NSData *dataToCache = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageURL]];

    if (dataToCache != nil) {
        [myCache setObject:dataToCache forKey:imageURL];
        [cell.imageShow setImage:[UIImage imageWithData:dataToCache]];
    }
}