混淆可可问题ߞ除非存在无法识别的方法调用,否则程序将挂起
请耐心等待,这个问题很难解释。我希望那里的某个英雄知道这里发生了什么。需要一些历史;
我的一个可可物体“球”代表一个小图形。它仅在视图内才有意义。在 Ball 的某些方法中,它要求视图重绘。最重要的是,只要设置了球的位置参数,它就会要求视图重绘。这是在设置器中实现的。
这是建议的内容:
In View.m
- (void)mouseUp:(NSEvent *)theEvent {
if (![runnerPath isEmpty]) {
[walkPath removeAllPoints];
[walkPath appendBezierPath:runnerPath];
[runnerPath removeAllPoints];
[[self held] setStep:0];
[[self held] setPath:walkPath];
[NSTimer scheduledTimerWithTimeInterval:.01 target:[self held] selector:@selector(pace) userInfo:nil repeats:YES];
}
}
In Ball.m
- (void)pace {
CGFloat juice = 10;
BOOL loop = YES;
while (loop) {
if ([self step] == [[self path] elementCount]) {
if ([[self timer] isValid]) {
[[self timer] invalidate];
}
[[self path] removeAllPoints];
// @throw([NSException exceptionWithName:@"test" reason:@"reason" userInfo:nil]);
}
if (loop) {
CGFloat distance;
NSPoint stepPoint;
if ([[self path] elementCount] > 0) {
NSPoint returnPoints[2];
[[self path] elementAtIndex:[self step] associatedPoints:returnPoints];
stepPoint = returnPoints[0];
distance = pixelDistance([self position], stepPoint);
}
if (distance <= juice) {
[self setPosition:stepPoint];
if (distance < juice) {
juice -= distance;
loop = YES;
[self setStep:[self step]+1];
} else {
loop = NO;
}
} else {
NSPoint cutPoint = moveAlongBetween([self position], stepPoint, juice);
[self setPosition:cutPoint];
loop = NO;
}
}
}
}
Bear with me, this one is hard to explain. I hope some hero out there knows what’s going on here. Some history needed;
One of my cocoa objects, “Ball” represents a small graphic. It only makes sense within a view. In some of the Ball’s methods, it asks the view to redraw. Most importantly, it asks the view to redraw whenever the Ball’s position parameter is set. This is achieved in the setter.
Here’s the mouthful, as suggested:
In View.m
- (void)mouseUp:(NSEvent *)theEvent {
if (![runnerPath isEmpty]) {
[walkPath removeAllPoints];
[walkPath appendBezierPath:runnerPath];
[runnerPath removeAllPoints];
[[self held] setStep:0];
[[self held] setPath:walkPath];
[NSTimer scheduledTimerWithTimeInterval:.01 target:[self held] selector:@selector(pace) userInfo:nil repeats:YES];
}
}
In Ball.m
- (void)pace {
CGFloat juice = 10;
BOOL loop = YES;
while (loop) {
if ([self step] == [[self path] elementCount]) {
if ([[self timer] isValid]) {
[[self timer] invalidate];
}
[[self path] removeAllPoints];
// @throw([NSException exceptionWithName:@"test" reason:@"reason" userInfo:nil]);
}
if (loop) {
CGFloat distance;
NSPoint stepPoint;
if ([[self path] elementCount] > 0) {
NSPoint returnPoints[2];
[[self path] elementAtIndex:[self step] associatedPoints:returnPoints];
stepPoint = returnPoints[0];
distance = pixelDistance([self position], stepPoint);
}
if (distance <= juice) {
[self setPosition:stepPoint];
if (distance < juice) {
juice -= distance;
loop = YES;
[self setStep:[self step]+1];
} else {
loop = NO;
}
} else {
NSPoint cutPoint = moveAlongBetween([self position], stepPoint, juice);
[self setPosition:cutPoint];
loop = NO;
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你能告诉我你是如何处理异常的吗?因为通常无法识别的选择器将结束您的程序。也许您需要一个异常而不是一个无法识别的选择器。尝试:
如果这也能解决问题,那么您将在这段代码之后执行一些操作,从而冻结应用程序。
编辑:感谢您的代码更新。
这里发生了一些奇怪的事情!我不打算重写整个事情,所以这里有一些提示:
while()
循环中暂停执行,因此无论如何它都会在一眨眼的时间内发生。您需要在类中保留一些状态信息。例如,每次调用pace
时添加一个循环计数器。-(void)pace:(NSTimer*)timer
,并使用timer
,而不是[self timer]
(后者无论如何,如果你不指定它,它就不会成为你的计时器!)-(void)pace:(NSTimer*)timer
,不要忘记使用@selector(pace:)
(即不要忘记:
)修复这些问题,如果仍然损坏,请再次更新您的问题并发表评论,以便我们知道。祝你好运!
could you also tell how you handle exceptions? since normally an unrecognized selector will end your program. Maybe you need an exception rather than an unrecognized selector. Try:
If this would fix it as well, you're doing something after this code which freezes the app.
edit: thanks for the code update.
There's some weird stuff going on here! I'm not going to rewrite the whole thing, so here's some pointers:
while()
loop, so it will happen in a blink anyway. You would need to keep some state information in the class. E.g. adding a loop counter every timepace
is called.-(void)pace:(NSTimer*)timer
, and usetimer
, not[self timer]
(the latter will not be your timer anyway, if you don't assign it!)-(void)pace:(NSTimer*)timer
, don't forget to use@selector(pace:)
(i.e. don't forget the:
)fix those things, and if it's still broken, update your question again and put in comment so we will know. Good luck!
尝试调用
我假设
views
是一个数组,因此快速枚举直接应用于它,无需调用 allObjects.其他几点。
Try calling
I'm assuming that
views
is an array, so fast enumeration applies to it directly and there is no need to call allObjects.A couple of other points.