为什么我的 CGPoint 不能正确保存在 NSMutable 数组中?

发布于 2024-11-15 14:17:35 字数 1642 浏览 4 评论 0原文

因此,我的代码基于以下位置找到的答案: 如何以简单的方式将 CGPoint 对象添加到 NSArray 中?

我的代码现在如下所示:

-(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?我用 点= [[NSMutableArray alloc] init]; 在 init 函数中,并且有 NSMutableArray *points;在我的 .h 文件中。我需要做更多的事情来启动 NSMutableArray 吗?

So, I've based my code off of the answer found at: How can I add CGPoint objects to an NSArray the easy way?

My code now reads like:

-(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

Does anybody have any idea what could possibly be going wrong?

edit:

I've noticed that anytime I check my points count, it's always at 0. Is there a way of checking whether I initialized my NSMutableArray correctly? I use
points = [[NSMutableArray alloc] init];
in the init function, and have NSMutableArray *points; in my .h file. Do I need to do anything more to initiate the NSMutableArray?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

橘亓 2024-11-22 14:17:35

我很惊讶这不会崩溃。您正在读取超出数组末尾的内容:

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

这应该是:

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

NSValue *val = [points lastObject];

I'm surprised this doesn't crash. You're reading beyond the end of the array:

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

This should be:

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

or

NSValue *val = [points lastObject];
幻梦 2024-11-22 14:17:35

[points objectAtIndex:[points count]] 始终超出范围。数组中的最后一个对象将位于索引 [points count] - 1 处。

或者,您可以只使用 NSValue *val = [points lastObject]

[points objectAtIndex:[points count]] will always be out of range. The last object in the array will be at index [points count] - 1.

Alternatively you could just use NSValue *val = [points lastObject].

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文