3

im having this error

    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) 

this is the code where im having this kind of error. dont know what's the prob.

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]];
}
David Rönnqvist
  • 56,267
  • 18
  • 167
  • 205
Ace Munim
  • 325
  • 3
  • 18

1 Answers1

4

You haven't given the full error but I guess you need to replace:

NSData *imageData = [myCache valueForKey:imageURL];

with

NSData *imageData = [myCache objectForKey:imageURL];

You might want to add some protection, like:

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

    if (dataToCache != nil) {
        [myCache setObject:dataToCache forKey:imageURL];
        [cell.imageShow setImage:[UIImage imageWithData:dataToCache]];
    }
}
Wain
  • 118,658
  • 15
  • 128
  • 151
  • done but im having this error now [NSCache setObject:forKey:cost:]: attempt to insert nil value (key: Put%20gravestones%20or%20"katba"%20on%20the%20graves%20of%20your%20loved%20ones,%20send%20wedding%20gifts,%20home%20appliances,%20and%20sell%20property,%20search%20gifts%20in%20rabwah%20by%20iRabwah%20services)' – Ace Munim Aug 20 '13 at 12:11
  • 1
    Where are you calling `setObject:forKey:cost:` ? And what are you trying to pass it ? You give it `nil` sometime. Not sure what all the stuff at the end of your comment is... – Wain Aug 20 '13 at 12:14
  • i never used setObject:forKey:cost and thats also im trying to figure out now coz i never declared or even called any. – Ace Munim Aug 20 '13 at 12:15
  • So it's when you call `[myCache setObject:dataToCache forKey:imageURL];`, looks like `dataToCache` is nil. – Wain Aug 20 '13 at 12:17
  • so does it mean the data that im setting into the cache is nil which im getting from imageURL and sending me back an error? any suggestion as to what to do? – Ace Munim Aug 20 '13 at 12:21
  • Yes. Check the URL you're downloading. Check what the response is. Check for `nil` before you try to save the result. – Wain Aug 20 '13 at 12:24
  • just checked it. and yes im having like 3 nil values. can i do like this to if(imageURL == nil) { imageURL = @"image.jpg";} ? – Ace Munim Aug 20 '13 at 12:38
  • so sorry to keep on bothering you about this.but it seems i cant get the hang to make this thing work :( – Ace Munim Aug 20 '13 at 12:48
  • just did it bro. i changed the nil value and set it to any other URL that i've choosen. – Ace Munim Aug 20 '13 at 13:15