3

Objective-Cの2つの異なるNSArrayからの値をペアにしてCGPointの配列を作成するにはどうすればよいですか?

値を持つ配列「A」があるとします。0, 1, 2, 3, 4
また、値を持つ配列「B」もあります。21, 30, 33, 35, 31

CGPoint値を使用して配列「AB」を作成したいと思います。(0,21), (1,30), (2,33), (3,35), (4,31)

ご協力いただきありがとうございます。

4

2 に答える 2

5

Objective-Cコレクションクラスはオブジェクトのみを保持できるため、入力番号NSNumberオブジェクトに保持されていると想定していることに注意してください。これは、 sが結合された配列のオブジェクトにCGPoint struct保持されている必要があることも意味します。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");
}
于 2013-02-20T14:03:02.727 に答える
2

他の誰かがCGPointのNSArrayの作成について投稿しましたが、あなたはCGPointの配列を要求しました。これはそれを行うべきです:

NSArray* a = @[ @(0.), @(1.), @(2.), @(3.), @(4.) ];
NSArray* b = @[ @(21.), @(30.), @(33.), @(35.), @(31.) ];

const NSUInteger aCount = a.count, bCount = b.count, count = MAX(aCount, bCount);
CGPoint* points = (CGPoint*)calloc(count, sizeof(CGPoint));
for (NSUInteger i = 0; i < count; ++i)
{
    points[i] = CGPointMake(i < aCount ? [a[i] doubleValue] : 0 , i < bCount ? [b[i] doubleValue] : 0.0);
}
于 2013-02-20T14:03:22.423 に答える