Vamshi Krishna Vamshi Krishna - 6 months ago 62
iOS Question

Merge Arrays to form Dictionaries - iOS Objective C

I have two 2D arrays like

Array1 = [(1, 2), (3, 4), (5, 6)]

Array2 = [(x, y), (x, y), (x, y)]

I want to form a dictionary like
"coordinates" = [{"x":"1", "y":"2"}, {"x":"3", "y":"4"},{"x":"5", "y":"6"}]

How do i do it?


As you mention in comment second array always have x and y then there is no need to loop through that array, You need just iterate first array like this.

NSMutableArray *dicArray = [[NSMutableArray alloc] init];
for (NSArray *subArray in array1) {
    NSDictionary *dic = @{ @"x": [subArray firstObject], @"y": [subArray lastObject] };
    [dicArray addObject:dic];