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.