How to add NSArray object which is located inside NSMutableDictionary and NSArray property?

sonoDamiano

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

Paul.s

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.

edited at
0

Comments

0 comments
Login to comment

Related

How to filter NSArray using predicate on an object property

How to add object to NSArray with key from database?

Sort NSArray by other NSMutableDictionary

How to add items from NSArray :

filter duplicate from NSArray using object property

Filter NSArray of objects to have unique property object

Add part of NSArray to other NSArray

How to implement KVO for readonly derived NSArray property?

How to sort a NSArray with another NSArray?

how to detect how many same object is in NSArray

How to add nil value to a NSArray in swift?

How to Add Multiple NSDictionaries to NSArray in objective c?

How to extract object from NSArray that is in Dictionary Format

Mutating NSDictionary inside NSArray

Updating NSDictionary inside an NSArray

Filter NSArray with NSObject inside

NSDictionary inside NSArray

Filter NSArray of Object with NSArray of NSNumber with NSPredicate

Simple NSArray - Bring object to to front NSArray

Swift - Insert object/item / Add object/item in NSArray

Convert NSData object to NSArray

How do I sort NSArrays by objects inside the objects inside the NSArray?

How to create NSDictionary inside NSArray and How to access them in ios

How do i create a function that will change the property inside an object that is located inside of an array, in a JSON

How can I change center of a view, which is stored in NSArray?

How to sort NSArray of custom objects by a specific property in descending order?

How to get NSArray object in NSString and send that object one by one?

How can I POST an NSArray of NSDictionaries inside an NSDictionary without problems?

How to filter NSArray in Swift?