Pregunta

How can I add item for the "kArrayOfItemsInTheBox" key in the structure like below?

NSArray* arrayOfBoxes = @[
                       [NSMutableDictionary dictionaryWithDictionary:@{
                                                                       kArrayOfItemsInTheBox : [NSArray arrayWithObjects:@"1",@"2", nil],
                                                                       kBoxNumber : @"Box nr 1",
                                                                       }
                        ],

                       [NSMutableDictionary dictionaryWithDictionary:@{
                                                                       kArrayOfItemsInTheBox : [NSArray arrayWithObjects:@"1",@"2", nil],
                                                                       kBoxNumber : @"Box nr 2",
                                                                       }
                        ]
                       ];
self.packageData = [NSMutableArray arrayWithArray:arrayOfBoxes];

I know how to display items from the array but I have a trouble with adding new object at the end of the existing list . I need to add this value by using packageData property. The final result after add new values should look like below structure:

 NSArray* arrayOfBoxes = @[
                       [NSMutableDictionary dictionaryWithDictionary:@{
                                                                       kArrayOfItemsInTheBox : [NSArray arrayWithObjects:@"1",@"2",@"Added Item", nil],
                                                                       kBoxNumber : @"Box nr 1",
                                                                       }
                        ],

                       [NSMutableDictionary dictionaryWithDictionary:@{
                                                                       kArrayOfItemsInTheBox : [NSArray arrayWithObjects:@"1",@"2", nil],
                                                                       kBoxNumber : @"Box nr 2",
                                                                       }
                        ]
                       ];

I try something like:

[[self.packageData objectAtIndex:myIndex]addObject:@{kArrayOfItemsInTheBox:@"NewValue" }];

If it is impossible to do maybe you have better idea how to create structure with the same logic. Thanks in advance

¿Fue útil?

Solución

The simplest way with your current structure is

NSMutableDictionary *item = [self.packageData objectAtIndex:myIndex];
item[kArrayOfItemsInTheBox] = [item[kArrayOfItemsInTheBox] arrayByAddingObject:newObject];

Generally as soon as I have to use constants for keys in a dictionary I consider creating a custom object instead

Otros consejos

First, the array you want to add to is immutable, so you need to change to use NSMutableArray.

Then, you need to navigate through your data structure. You have an array of dictionaries containing and array and a string. You need to dig into this structure. Your current code digs into the outer array, but then tries to treat the dictionary as an array.

Try (after making the inner arrays mutable):

[[[self.packageData objectAtIndex:myIndex] objectForKey:kArrayOfItemsInTheBox] addObject:@"NewValue"];

though it is better to break this line out into parts so it's easier to understand (and hence, see where the error is).

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top