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
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
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments