【问题标题】:Add NSMutableDictionary into Array for Button Action将 NSMutableDictionary 添加到数组中以进行按钮操作
【发布时间】:2016-12-21 18:25:01
【问题描述】:

我有一个 NSMutableDictionary:

sampleDict = [NSMutableDictionary new];

[sampleDict setObject:@"FooIndex" forKey:@"Key_1"]; // adds @"Foo"
[sampleDict setObject:@"FooOne" forKey:@"Key_2"]; // adds @"Foo"
[sampleDict setObject:@"FooTwo" forKey:@"Key_3"]; // adds @"Foo"
[sampleDict setObject:@"FoFour" forKey:@"Key_4"]; // adds @"Foo"
[sampleDict setObject:@"FooFivve" forKey:@"Key_5"]; // adds @"Foo"

我想将此字典添加到 Array on Button 操作这里的代码是这样的,

arraytesting = [NSMutableArray new];

- (IBAction)action:(id)sender {
    [arraytesting addObject:sampleDict];
    NSLog(@"Sample arraytesting>>>>> %@",arraytesting);

}

最后的输出是:

(
        {
        "Key_1" = FooIndex;
        "Key_2" = FooOne;
        "Key_3" = FooTwo;
        "Key_4" = FoFour;
        "Key_5" = FooFivve;
    }
)

但是当我需要更新字典时:

[sampleDict setObject:@"one" forKey:@"Key_1"]; // adds @"Foo"
[sampleDict setObject:@"two" forKey:@"Key_2"]; // adds @"Foo"
[sampleDict setObject:@"three" forKey:@"Key_3"]; // adds @"Foo"
[sampleDict setObject:@"FoFour" forKey:@"Key_4"]; // adds @"Foo"
[sampleDict setObject:@"FooFivve" forKey:@"Key_5"]; // adds @"Foo"

输出如下:

(
    {
        "Key_1" = FooIndex;
        "Key_2" = FooOne;
        "Key_3" = FooTwo;
        "Key_4" = FoFour;
        "Key_5" = FooFivve;
    },
    {
        "Key_1" = FooIndex;
        "Key_2" = FooOne;
        "Key_3" = FooTwo;
        "Key_4" = FoFour;
        "Key_5" = FooFivve;
    }
)

我希望输出是这样的:

(
    {
        "Key_1" = FooIndex;
        "Key_2" = FooOne;
        "Key_3" = FooTwo;
        "Key_4" = FoFour;
        "Key_5" = FooFivve;
    },
    {
        "Key_1" = one;
        "Key_2" = two;
        "Key_3" = three;
        "Key_4" = FoFour;
        "Key_5" = FooFivve;
    }
)

【问题讨论】:

    标签: ios objective-c nsmutablearray nsmutabledictionary


    【解决方案1】:

    向数组添加对对象的引用。之后,您编辑您的字典并再次添加它的引用。您可以调用copy 来创建sampleDict 的新实例。 你的方法必须是这样的:

    - (IBAction)action:(id)sender { [arraytesting addObject:[sampleDict copy]]; NSLog(@"Sample arraytesting>>>>> %@",arraytesting); }
    

    【讨论】:

    • 感谢您的回答@PRECover,但面临同样的问题
    【解决方案2】:
    [sampleDict setObject:@"one" forKey:@"Key_1"]; // adds @"Foo"
    [sampleDict setObject:@"two" forKey:@"Key_2"]; // adds @"Foo"
    [sampleDict setObject:@"three" forKey:@"Key_3"]; // adds @"Foo"
    [sampleDict setObject:@"FoFour" forKey:@"Key_4"]; // adds @"Foo"
    [sampleDict setObject:@"FooFivve" forKey:@"Key_5"]; // adds @"Foo"
    

    在这里,您使用的是相同 sampleDict 的相同参考。而不是再次创建一个新对象并添加。

    smapleDict = [NSMutableArray new]

    [sampleDict setObject:@"one" forKey:@"Key_1"]; // adds @"Foo"
        [sampleDict setObject:@"two" forKey:@"Key_2"]; // adds @"Foo"
        [sampleDict setObject:@"three" forKey:@"Key_3"]; // adds @"Foo"
        [sampleDict setObject:@"FoFour" forKey:@"Key_4"]; // adds @"Foo"
        [sampleDict setObject:@"FooFivve" forKey:@"Key_5"]; // adds @"Foo"
    

    【讨论】:

    • 我不明白@sivajee
    • 您正在对同一个字典进行更改。那是你得到相同的元素
    • 这里我只想使用一个字典,不需要再创建一个
    【解决方案3】:

    您的字典sampleDict 是一个引用,因此您需要对其进行可变副本,如果您不修改原始字典,这将修改您数组上的所有字典,因此您需要复制、修改和添加然后到你的数组,像这样

    #import "ViewController.h"
    
    @interface ViewController ()
    @property NSMutableArray * arrayTesting;
    @property NSMutableDictionary * sampleDict;
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        self.sampleDict = [NSMutableDictionary new];
    
    
        [self.sampleDict setObject:@"FooIndex" forKey:@"Key_1"]; // adds @"Foo"
        [self.sampleDict setObject:@"FooOne" forKey:@"Key_2"]; // adds @"Foo"
        [self.sampleDict setObject:@"FooTwo" forKey:@"Key_3"]; // adds @"Foo"
        [self.sampleDict setObject:@"FoFour" forKey:@"Key_4"]; // adds @"Foo"
        [self.sampleDict setObject:@"FooFivve" forKey:@"Key_5"]; // adds @"Foo"
    
        self.arrayTesting = [NSMutableArray new];
    
        [self.arrayTesting addObject:self.sampleDict];
        NSLog(@"Sample arraytesting>>>>> %@",self.arrayTesting);
    }
    
    - (IBAction)buttonAction:(id)sender {
    
    
    
        NSMutableDictionary * toModify = [self.sampleDict mutableCopy];
    
        [toModify setObject:@"One" forKey:@"Key_1"]; // adds @"Foo"
        [toModify setObject:@"Two" forKey:@"Key_2"]; // adds @"Foo"
        [toModify setObject:@"Three" forKey:@"Key_3"]; // adds @"Foo"
        [toModify setObject:@"FoFour" forKey:@"Key_4"]; // adds @"Foo"
        [toModify setObject:@"FooFivve" forKey:@"Key_5"]; // adds @"Foo"
    
        [self.arrayTesting addObject:toModify];
        NSLog(@"Sample arraytesting>>>>> %@",self.arrayTesting);
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    
    @end
    

    希望对你有帮助

    【讨论】:

    • 谢谢@reinier,这只是一次更新,但在这里我想在每个按钮操作时更新字典上的新值
    • 同理,更改字典新的可变副本中的值,然后添加到数组中
    • 您从什么地方获取数据?您可以发布所有 viewController 代码以了解您在尝试什么吗?
    猜你喜欢
    • 2016-05-15
    • 2012-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-30
    相关资源
    最近更新 更多