Create an array of CGPoints by pairing values from two different NSArrays

cgpoint, ios, nsarray, objective-c

Solution

Note that Objective-C collection classes can only hold objects, so I have assumed your input numbers are held in `NSNumber` objects. This also means that the `CGPoint` `struct`s must be held in a `NSValue` object in the combined array:

NSArray *array1 = ...;
NSArray *array2 = ...;
NSMutableArray *pointArray = [[NSMutableArray alloc] init];

if ([array1 count] == [array2 count])
{
    NSUInteger count = [array1 count], i;
    for (i = 0; i < count; i++)
    {
        NSNumber *num1 = [array1 objectAtIndex:i];
        NSNumber *num2 = [array2 objectAtIndex:i];
        CGPoint point = CGPointMake([num1 floatValue], [num2 floatValue]);
        [pointArray addObject:[NSValue valueWithCGPoint:point]];
    } 
}
else
{
    NSLog(@"Array count mis-matched");
}

Problem

How to create an array of CGPoints by pairing values from two different NSArrays in objective-c? Lets say I have an Array "A" with the values: `0, 1, 2, 3, 4` And I also have an Array "B" with the values: `21, 30, 33, 35, 31` I would like to create Array "AB" with CGPoint values: `(0,21), (1,30), (2,33), (3,35), (4,31)` Thanks for your help.

Original source