key/value returns nil on phone only

Discussion in 'iOS Programming' started by nashyo, Apr 25, 2012.

  1. nashyo macrumors 6502

    nashyo

    Joined:
    Oct 1, 2010
    Location:
    Bristol
    #1
    My code works fine on the simulator. However, I'm getting this error message when running on the iPhone 4S:

    2012-04-25 12:30:24.702 Design3RCGP[1812:707] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary setObject:forKey:]: attempt to insert nil value (key: cellImage)'

    For this code:
    Code:
    - (void)viewDidLoad
    {
        NSMutableDictionary *dictionary1 = [NSMutableDictionary dictionaryWithCapacity:3];
        NSMutableDictionary *dictionary2 = [NSMutableDictionary dictionaryWithCapacity:3];
        NSMutableDictionary *dictionary3 = [NSMutableDictionary dictionaryWithCapacity:3];
            
        [dictionary1 setObject:[[NSString alloc] initWithString:@"Podcasts"] forKey:@"cellName"];
        [dictionary1 setObject:[UIImage imageNamed:@"PodcastIcon"] forKey:@"cellImage"];
        
        [dictionary2 setObject:[[NSString alloc] initWithString:@"Briefings"] forKey:@"cellName"];
        [dictionary2 setObject:[UIImage imageNamed:@"essentialicon"] forKey:@"cellImage"];
        
        [dictionary3 setObject:[[NSString alloc] initWithString:@"Essential Updates"] forKey:@"cellName"];
        [dictionary3 setObject:[UIImage imageNamed:@"EKnUpdIcon"] forKey:@"cellImage"];
        
        array = [[NSArray alloc] initWithObjects:
                  dictionary1,
                  dictionary2,
                  dictionary3,
                  nil];
    }
    
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        return 1;
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return [array count];
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static Temp1Cell *cell;
        static NSString *CellIdentifier = @"Cell";
        
        if (!cell) {
            cell = [[Temp1Cell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }
        
        cell = (Temp1Cell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        
        NSDictionary *dictionary = [array objectAtIndex:indexPath.row];
        
        cell.mainLabel.text = [dictionary objectForKey:@"cellName"];
        cell.imageView.image = [dictionary objectForKey:@"cellImage"];
        
        return cell;
    }
    This works fine on the simulator. Why?
     
  2. cMacSW macrumors regular

    Joined:
    Mar 20, 2006
    #2
    Your code is failing on one of these lines:
    Code:
        [dictionary1 setObject:[UIImage imageNamed:@"PodcastIcon"] forKey:@"cellImage"];
        
        [dictionary2 setObject:[UIImage imageNamed:@"essentialicon"] forKey:@"cellImage"];
        
         [dictionary3 setObject:[UIImage imageNamed:@"EKnUpdIcon"] forKey:@"cellImage"];
    
    Because you're not loading the image successfully, hence trying to insert nil into the dictionary
     

Share This Page