junyi00
junyi00

Reputation: 792

NSMutableDictionary setValue:forKey failing sometimes

I am using a NSMutableDictionary to hold 3 key/value pairs. The value is bool in a form of NSNumber. Then I have a table view that loads the data, if the cell is pressed, the value is changed from YES to NO or vice versa.

It seems that the value is changed correctly when the cell is pressed, except for the last key/value pair in the dictionary

NSMutableDictionary *modsDict;
NSString *test = @"test";
NSString *test1 = @"test1";
NSString *test2 = @"test2";

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    NSString *key = [NSString stringWithFormat: @"%@", [[modsDict allKeys] objectAtIndex: indexPath.row]];

    if (cell.accessoryType == UITableViewCellAccessoryNone) {
        //enable
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        [modsDict setValue: [NSNumber numberWithBool: 1] forKey: key];
    }
    else {
        cell.accessoryType = UITableViewCellAccessoryNone;
        [modsDict setValue: [NSNumber numberWithBool: 0] forKey: key];
    }

}

-(void)viewDidLoad {

    %orig;

    modsDict = [[NSMutableDictionary alloc] init];
    [modsDict setObject: [NSNumber numberWithBool: 0] forKey:test];
    [modsDict setObject: [NSNumber numberWithBool: 0] forKey:test1];
    [modsDict setObject: [NSNumber numberWithBool: 0] forKey:test2];

    ... //init tableview

}

-(void)someMethodThatIHookTo {
    NSNumber *val = [modsDict objectForKey: test2];
    if ([val boolValue] == YES) {
        //do something
    }
    //this works for test and test1, but for test2 it crashed

   bool lol = [[modsDict objectForKey: test2] boolValue];
   if (lol) {
       //so something 
   }
   else {

   }
   //else is always called

   if ([[modsDict objectForKey: test2] boolValue]) {
   }
   else { }
   //if statement always true, never goes to else
}

These are the three ways I tried to take value from the NSDictionary I am doing [NSNumber numberWithBool: 0/1] because if I use NO/YES it would return as YES even if NO is used.

No matter how many time I have pressed the cell, The result from the 3 ways is the same...

How can I solve this?

Please note that I am using this on THEOS, mobilesubtrate as a tweak.

Upvotes: 0

Views: 322

Answers (1)

Tommy
Tommy

Reputation: 100632

I think perhaps you need to read the documentation a little harder.

Here's what the documentation for allKeys says:

Discussion
The order of the elements in the array is not defined.

With that in mind, what result do you think the following fragment from your code will produce?

[[modsDict allKeys] objectAtIndex: indexPath.row]

That's probably why you're inventing superstitious beliefs like:

I am doing [NSNumber numberWithBool: 0/1] because if I use NO/YES it would return as YES even if NO is used.

For the record:

  • it's idiomatic just to use @YES and @NO;
  • [NSNumber numberWithBool:0] and [NSNumber numberWithBool:NO] will return not only numbers of the same value but will return numbers with exactly the same identity due to a special case related to NSNumber Booleans. Ditto for YES/non-zero.

The fix for your code is to stop making assumptions about the order of something with an undefined order. Probably just use an array, as it's unclear why you're using a dictionary anyway.

Upvotes: 3

Related Questions