如何更新NSMutableDictionary中的对象值而不复制项

时间:2015-10-28 15:55:00

标签: ios objective-c nsmutabledictionary uiswitch

如果此问题与one I asked previously非常相似,请道歉。但是,我遇到了一个我以前没有遇到过的新问题。

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

最终目的是让用户能够使用UISwitch进行选择并跟踪这些切换选择的值,然后将其保存到Core Data。

这几乎完美无缺,除非用户与交换机交互,否则它会向NSMutableDictionary添加一个全新的对象和密钥

这是我用来构建UI并更新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

1 个答案:

答案 0 :(得分:0)

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

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

小时。

@property (nonatomic, strong) NSString *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 *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);
}