-1

* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* setObjectForKey: key cannot be nil' ***

I think i am trying to add an object (which is nil) to dictionary for key. I DO NOT KNOW HOW TO SOLVE THIS?

 @interface BNRImageStore ()
@property (nonatomic, strong) NSMutableDictionary *dictionary;

@end

@implementation BNRImageStore


+(instancetype)sharedStore{
    static BNRImageStore *sharedStore;

    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedStore = [[self alloc]initPrivate];
    });
    return sharedStore;
}

-(instancetype)initPrivate {
    self = [super init];
    if (self) {
        _dictionary = [[NSMutableDictionary alloc]init];
    }
    return self;
}

-(NSString*)imagePathForKey:(NSString*)key{
    NSArray *documentDirectories =
        NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                            NSUserDomainMask,
                                            YES);

    NSString *documentDirectory = [documentDirectories firstObject];
    return [documentDirectory stringByAppendingPathComponent:key];
}
-(void)setImage:(UIImage*)image forKey:(NSString*)key{
    self.dictionary[key] = image;


    NSString *imagePath = [self imagePathForKey:key];
    NSData *data = UIImageJPEGRepresentation(image, 0.5);

    [data writeToFile:imagePath atomically:YES];

}
-(UIImage*)imageForKey:(NSString*)key{
    UIImage *result = self.dictionary[key];

   if (!result) {
     NSString *imagePath = [self imagePathForKey:key];

 result = [UIImage imageWithContentsOfFile:imagePath];

 if (result) {
     [ self.dictionary setObject:result forKey:key];


  }
   else {
       NSLog(@"ERROR");

   }
    }
    return result;

}

-(void)deleteImageForKey:(NSString*)key{
    if (!key) {
        return;
    }

    [self.dictionary removeObjectForKey:key];

    NSString *imagePath = [self imagePathForKey:key];
    [[NSFileManager defaultManager]removeItemAtPath:imagePath error:nil];

}

@end
Brad
  • 11,934
  • 4
  • 45
  • 73
Wenjun A Mao
  • 41
  • 1
  • 3
  • 1
    Whick line is causing the error? And the provlem is that key, not the value, is nil. – rmaddy Aug 06 '15 at 04:06
  • How do i make the key non- nil? Sorry, i'm quite new at this! @rmaddy – Wenjun A Mao Aug 06 '15 at 04:09
  • Start by pointing out which line of code in your question is causing the error. – rmaddy Aug 06 '15 at 04:10
  • @rmaddy i think it might be the key in line 18? i'm not completely sure – Wenjun A Mao Aug 06 '15 at 04:15
  • 1
    There are no line numbers in your question. And how can you not be sure which line it is? Run the app in the debugger and see where it crashes. Xcode will show you exactly where. If not, please see http://www.raywenderlich.com/10209/my-app-crashed-now-what-part-1 – rmaddy Aug 06 '15 at 04:17
  • Line 18 in the code you've posted is a closing brace, so it's probably not that one. – jscs Aug 06 '15 at 05:17
  • the Exception went away, but the image doesn't display anymore? How do you get the image to display? @liushuaikobe – Wenjun A Mao Aug 06 '15 at 05:33

1 Answers1

0

According to the information, setObjectForKey: key cannot be nil, so you need to check the key and value to make sure that neither of them is nil before your setObject:ForKey:.

Add some protection like this:

-(void)setImage:(UIImage*)image forKey:(NSString*)key{
    // Add this protection
    if (!image || !key) {
        return;
    }
    self.dictionary[key] = image;

    NSString *imagePath = [self imagePathForKey:key];
    NSData *data = UIImageJPEGRepresentation(image, 0.5);

    [data writeToFile:imagePath atomically:YES];

}
liushuaikobe
  • 2,152
  • 1
  • 23
  • 26
  • it still raises the same exception: *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** setObjectForKey: key cannot be nil' *** First throw call stack: – Wenjun A Mao Aug 06 '15 at 03:47
  • @WenjunAMao So find out all your `setObjectForKey` and add the protection of object not nil. – liushuaikobe Aug 06 '15 at 03:49
  • The key is nil, not the object. – rmaddy Aug 06 '15 at 04:16
  • FYI - code-only answers are frowned upon here. Please add some explanation of what was wrong and what your answer does to fix it. – rmaddy Aug 06 '15 at 04:18