如何通过配对 objective-c中两个不同NSArray的值来创建一个CGPoints数组? 假设我有一个数组“A”,其值为:0,1,2,3,4 我还有一个数组“B”,其值为:21,30,33,35,31 我想用CGPoint值创建数组“AB”:
假设我有一个数组“A”,其值为:0,1,2,3,4
我还有一个数组“B”,其值为:21,30,33,35,31
我想用CGPoint值创建数组“AB”:(0,21),(1,30),(2,33),(3,35),(4,31)
谢谢你的帮助.
请注意,Objective-C集合类只能保存对象,因此我假设您的输入数字保存在NSNumber对象中.这也意味着CGPoint结构必须保存在组合数组中的NSValue对象中: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");
}
