AKSHAY TALWEKAR
AKSHAY TALWEKAR

Reputation: 11

NSDictionary inside NSArray

I am trying to put different instances of 1 dictionary into an array. Shared the expected and current output. Please suggest something Thanks in advance

Program.h

@property (nonatomic,strong)NSMutableArray* array;

Program.m

-- (void)StartFuntion
 {
_array = [[NSMutableArray alloc] init];
NSMutableDictionary* dicOne = [[NSMutableDictionary alloc] init];

[dicOne setObject:@"A" forKey:@"1"];        //Adding new
[dicOne setObject:@"B" forKey:@"2"];
[dicOne setObject:@"C" forKey:@"3"];
[dicOne setObject:@"D" forKey:@"4"];
[self addob:dicOne];                       //sending dicOne to funtion 

[dicOne removeAllObjects];              //Remove objects

[dicOne setObject:@"X" forKey:@"100"];      //Again Adding new objects
[dicOne setObject:@"Y" forKey:@"200"];
[dicOne setObject:@"Z" forKey:@"300"];
[dicOne setObject:@"W" forKey:@"400"];
[self addob:dicOne];
}


-- (void) addob:(NSMutableDictionary*)dic{

[_array addObject:dic];
NSLog(@"_array = %@",_array);

}

Current Output:

  _array = (
         {
    100 = X;
    200 = Y;
    300 = Z;
    400 = W;
},
    {
    100 = X;
    200 = Y;
    300 = Z;
    400 = W;
}
    )

Expected output:

  _array = (
     {
    1 = A;
    2 = B;
    3 = C;
    4 = D;
},
    {
    100 = X;
    200 = Y;
    300 = Z;
    400 = W;
  }
      )

Upvotes: 0

Views: 185

Answers (4)

Frédéric T.
Frédéric T.

Reputation: 66

Sure you can, but you have to make a copy of dic before inserting it in _array.

-- (void) addob:(NSMutableDictionary*)dic{
[_array addObject:[dic copy]];
NSLog(@"_array = %@",_array);
}

or use [dic mutableCopy] if you want mutable dict in your array. You can also make the copy when you call addob: and not in the method.

Upvotes: 0

Tonton Sevilla
Tonton Sevilla

Reputation: 11

You can do something like this:

NSMutableArray array = [[NSMutableArray alloc] init];
[array addObject:dict1];
[array addObject:dict2];

Upvotes: 0

Avi
Avi

Reputation: 7552

You are reusing an NSMutableDictionary instead of creating a new instance. Do something like this instead...

[_array addObject:@{
                    @"1" : @"A",
                    @"2" : @"B",
                    @"3" : @"C",
                    @"4" : @"D",
                   }];

[_array addObject:@{
                    @"100" : @"X",
                    @"200" : @"Y",
                    @"300" : @"Z",
                    @"400" : @"W",
                   }];

Upvotes: 1

rmaddy
rmaddy

Reputation: 318774

You can't reuse dicOne. Instead of removing its objects, create a new instance.

_array = [[NSMutableArray alloc] init];
NSMutableDictionary* dicOne = [[NSMutableDictionary alloc] init];

[dicOne setObject:@"A" forKey:@"1"];        //Adding new
[dicOne setObject:@"B" forKey:@"2"];
[dicOne setObject:@"C" forKey:@"3"];
[dicOne setObject:@"D" forKey:@"4"];
[self addob:dicOne];                       //sending dicOne to funtion 

dicOne = [[NSMutableDictionary alloc] init];

[dicOne setObject:@"X" forKey:@"100"];      //Again Adding new objects
[dicOne setObject:@"Y" forKey:@"200"];
[dicOne setObject:@"Z" forKey:@"300"];
[dicOne setObject:@"W" forKey:@"400"];
[self addob:dicOne];

Upvotes: 3

Related Questions