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?
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];
}
NSLog(@"%@",dicArray);
Another Alternative:
NSMutableArray *finalArray = [[NSMutableArray alloc] init];
for (NSArray* arr1 in Array1) {
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
for (NSArray* arr2 in Array2) {
[dict setValue:arr1.firstObject forKey:arr2.firstObject];
[dict setValue:arr1.lastObject forKey:arr2.lastObject];
}
[finalArray addObject:dict];
}
NSLog(@"%@", finalArray);