0

だから、私は次の場所にある答えに基づいてコードを作成しました: How can I add CGPoint objects to an NSArray the easy way?

私のコードは次のようになります。

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch * touch = [touches anyObject];
    locationOne = [touch locationInView: [UIApplication sharedApplication].keyWindow];
    NSLog(@"In touchesBegan");
    NSLog(@"x is: %.3f.  y is: %.3f", locationOne.x, locationOne.y);
    [points addObject:[NSValue valueWithCGPoint:locationOne]];
    NSValue *val = [points objectAtIndex:[points count]];
    CGPoint p = [val CGPointValue];
    NSLog(@"in points array:  x is: %.3f, y is: %.3f", p.x ,p.y);
}

2011-06-16 13:21:57.367 canvas_test[7889:307] In touchesBegan
2011-06-16 13:21:57.371 canvas_test[7889:307] x is: 115.000.  y is: 315.500
2011-06-16 13:21:57.374 canvas_test[7889:307] in points array:  x is: 0.000, y is: 0.000
2011-06-16 13:22:00.982 canvas_test[7889:307] In touchesBegan
2011-06-16 13:22:00.985 canvas_test[7889:307] x is: 274.500.  y is: 386.000
2011-06-16 13:22:00.988 canvas_test[7889:307] in points array:  x is: 0.000, y is: 0.190
2011-06-16 13:22:11.476 canvas_test[7889:307] In touchesBegan
2011-06-16 13:22:11.480 canvas_test[7889:307] x is: 105.500.  y is: 140.500
2011-06-16 13:22:11.483 canvas_test[7889:307] in points array:  x is: 0.000, y is: 0.190

何が間違っている可能性があるのか​​ 誰にも分かりますか?

編集:

ポイント数を確認すると、常に 0 になっていることに気付きました。NSMutableArray を正しく初期化したかどうかを確認する方法はありますか? points = [[NSMutableArray alloc] init]; を使用します。init 関数で、NSMutableArray *points を持っています。私の.hファイルに。NSMutableArray を開始するために他に何かする必要がありますか?

4

2 に答える 2

3

これがクラッシュしないことに驚いています。配列の終わりを超えて読んでいます:

NSValue *val = [points objectAtIndex:[points count]];

これは次のようになります。

NSValue *val = [points objectAtIndex:[points count] - 1];

また

NSValue *val = [points lastObject];
于 2011-06-16T20:30:42.590 に答える
1

[points objectAtIndex:[points count]]常に範囲外になります。配列の最後のオブジェクトは index になります[points count] - 1

または、単に使用することもできますNSValue *val = [points lastObject]

于 2011-06-16T20:29:44.810 に答える