如何在不复制项目的情况下更新NSMutableDictionary中的对象的值

问题描述:

如果此问题与one I asked previously非常相似,请致歉。但是,我遇到了一个我以前没有遇到的新问题。如何在不复制项目的情况下更新NSMutableDictionary中的对象的值

所以,我有一堆UISwitch元素是根据存储在plist中的值(标签和ID号)动态创建的。我基本上阅读plist文件并获得一系列项目,并为每个项目创建UISwitch。这部分工作很好。

最终目的是使用户能够使用UISwitch进行选择并跟踪这些开关选择的值,然后将它们保存到核心数据中。

这几乎是可以正常使用,但每当用户与交换机进行交互,它增加了一个全新的目标和关键的NSMutableDictionary

这里是我的代码块我用它来构建用户界面和更新的NSMutableDictionary。

.H

@property int yPosStart; 
@property NSString *switchValue; 
@property (nonatomic, strong) UISwitch *qualifierSwitch; 
@property (nonatomic, strong) NSMutableDictionary *evqValuesDict; 

的.m

for (int i =0; i < [evqArray count]; i++) 
{ 
    id selector = [evqArray objectAtIndex:i]; 
     NSLog(@"selector: %@", selector); 

    NSString *labelText = selector[@"label"]; 
     NSLog(@"labelText: %@", labelText); 

    NSString *evqQualifierID = selector[@"event_qualifier_id"]; 
     NSLog(@"evqQualifierID: %@", evqQualifierID); 

    NSInteger b = [evqQualifierID integerValue]; 

    // for every item in the array, add 35 pixels to the xPosStart 
    _yPosStart = _yPosStart + 35; 

    // Convert integer into CGFloat for positioning 
    CGFloat yPosition = _yPosStart; 

    UILabel *switchLabel = [[UILabel alloc] initWithFrame:CGRectMake(60, yPosition, 240, 27)]; 

    [switchLabel setText:labelText]; 

    _qualifierSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(0, yPosition, 0, 0)]; 

    // add an incrementing tag so the siwtch interactions are unique 
    _qualifierSwitch.tag = b; 

    [myScroll addSubview:_qualifierSwitch]; 

    [myScroll addSubview:switchLabel]; 

    //Matcht the saved value to the switch 
    _dbSwitchValue = [_evqSavedValues objectForKey:evqQualifierID]; 
     NSLog(@"myString2: %@", _dbSwitchValue); 

    if ([_dbSwitchValue isEqualToString:@"Yes"]) { 
     [_qualifierSwitch setOn:YES animated:YES]; 
    } else { 
     [_qualifierSwitch setOn:NO animated:YES]; 
    } 

    if (_qualifierSwitch.on) 
    { 
     _editSwitchValue = @"Yes"; 

    } else { 
     _editSwitchValue = @"No"; 
    } 

    [_qualifierSwitch addTarget:self action:@selector(evqChangeSwitch:) forControlEvents:UIControlEventTouchUpInside]; 

    [_evqValuesDict setObject:_editSwitchValue forKey:evqQualifierID]; 

} 

- (void)evqChangeSwitch:(id)sender { 
NSLog(@"evqSwitch: %@", _qualifierSwitch); 
NSLog(@"_evqValuesDict at start of method: %@", _exqValuesDict); 

//get the sender's tag value 
NSInteger i = [sender tag]; 

//cast the tag value into a string for comparison to the string value for purposes of matching the dictionary key 
NSString *tagID = [NSString stringWithFormat:@"%ld", (long)i]; 

NSLog (@"tagID: %@:", tagID); 

if([sender isOn]){ 

    NSLog(@"Switch is ON for tagID: %@", tagID); 
    //Set string for display purposes elsewhere 
    switchValue = @"Yes"; 
    [_exqValuesDict removeObjectForKey:tagID]; 
    [_evqValuesDict setObject:switchValue forKey:tagID]; 

} else { 

    NSLog(@"Switch is OFF for tagID: %@", tagID); 
    //Set string for display purposes elsewhere 
    switchValue = @"No"; 
    [_exqValuesDict removeObjectForKey:tagID]; 
    [_evqValuesDict setObject:switchValue forKey:tagID]; 

} 

NSLog(@"_evqValuesDict at end of method: %@", _evqValuesDict); 

}

下面是在evqChangeSwitch方法开始日志,并在结束。

_evqValuesDict at start of method: { 
3 = No; 
1 = No; 
2 = No; 
} 

evqValuesDict at end of method: { 
3 = No; 
2 = No; 
2 = Yes; 
1 = No; 
} 

因此您可以看到,对于每个已选择的交换机,似乎都会创建一个副本。

一旦新的,明显重复的值在NSMutableDictionary中,则该键的值实际上会正确更新。

我建立和反对iOS9测试和运行的XCode 7.1

+0

而不是删除和密钥附加价值的,你可以尝试更新键的值就是这样,'_exqValuesDict [标签识别] = switchValue' – Vijay

+0

我会尝试,并让你知道会发生什么。 –

+0

对字典的值没有影响。这几乎就像这些物品本身是不可变的。我做了一个测试,只尝试从字典中删除项目,没有任何反应。或者,也许有一些关于发送UISwitch.tag的整数值并将其转换回一个字符串,使它与我试图在字典中引用的密钥有所不同。 –

通过简化一切就迎刃而解了。而我认为它不工作,因为我试图使用一个新的字符串(新的内存指针)来更新键的对象。所以我的猜测是(没有完全研究这个)是当NSMutableDictionary做setValue时:对于Key:它引用对象的内存地址,而不是字符串值。

我做的主要改变是使用单个属性evqQualifierID来跟踪字典键。 h。

h。

@property (nonatomic, strong) NSString *evqQualifierID; 

m。

- (void)evqChangeSwitch:(id)sender { 
NSLog(@"evqSwitch: %@", _qualifierSwitch); 
NSLog(@"_evqValuesDict at start of method: %@", _exqValuesDict); 

//get the sender's tag value 
NSInteger i = [sender tag]; 

//cast the tag value into a string for comparison to the string value for purposes of matching the dictionary key 
NSString *tagIDString = [NSString stringWithFormat:@"%ld", (long)i]; 

NSLog (@"tagIDString: %@:", tagIDString); 

if([sender isOn]){ 

    NSLog(@"Switch is ON for tagIDString: %@", tagIDString); 
    //Set string for display purposes elsewhere 
    switchValue = @"Yes"; 

} else { 

    NSLog(@"Switch is OFF for tagIDString: %@", tagIDString); 
    //Set string for display purposes elsewhere 
    switchValue = @"No"; 
} 

[_evqValuesDict setObject:switchValue forKey:_evqQualifierID]; 

NSLog(@"_evqValuesDict at end of method: %@", _evqValuesDict); 
}