【问题标题】:"missing context for method declaration" [duplicate]“方法声明缺少上下文” [重复]
【发布时间】:2013-04-02 05:50:36
【问题描述】:

在我的代码中,两个 -(void) 语句都出现“missing context for method declaration”错误,我无法找出原因。

这是我的代码:

- (void)onTimer {
[self checkCollision];
bad.center = CGPointMake(bad.center.x+pos.x,bad.center.y+pos.y);
if(bad.center.x >320 || bad.center.x<0)
    pos.x = -pos.x;
if(bad.center.y >480 || bad.center.y<0)
    pos.y = -pos.y;
}

- (void) checkCollision {
if (CGRectIntersectsRect(good.frame,bad.frame)) {
    [startButton setHidden:NO];
    [timer invalidate];

    CGRect frame = [good frame];
    frame.origin.x = 132.0;
    frame.origin.y = 332.0;
    [good setFrame:frame];

    CGRect frame2 = [good frame];
    frame2.origin.x = 125.0;
    frame2.origin.y = 70.0;
    [bad setFrame:frame2]; 

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Your not fast enough!"                         message:@"Try again!" delegate:nil cancelButtonTitle:@"Dismiss"otherButtonTitles:nil];

    [alert show];
    [alert release];
}
@end

【问题讨论】:

  • 我们刚刚回答了您的这个问题,就像半小时前一样。不要告诉我你不能从中推断出这个问题的解决方案,只是这次缺少的是@implementation MyClassName。不要来这里要求我们解决你每三十分钟遇到的每一个语法错误,所以不是为了那个目的。努力学习该语言的基础知识。
  • 我讨厌在这里发布我的诚实问题而受到这样的抨击,下次我会在其他地方问,给您带来的不便很抱歉
  • 另外,我敦促您遵循我从其他问题中提出的简单建议,即找到一些工作代码并将您自己的代码与它进行比较。您可能会找到解决方案,并学到一些东西。如果您尝试这样做,但即使在搜索之后仍然无法得到它,那可能意味着您有一个很好的候选人来回答一个新问题。但现在看起来你只是在向全世界宣布你不想做任何事情来帮助自己,这就是你被否决的原因。
  • 首先,也可能是最后一点,@implementation MyClassName 部分实际上是不需要的,你错了。我有一个类似的问题,我有两个 .h 文件。其中一个提出了 OP 询问的错误。另一个没有。一定是其他问题。

标签: objective-c


【解决方案1】:

现在在这种情况下;

你没有,@implementation &lt;ClassName&gt; 在上面。

错误"missing context for method declaration"只是由于这个原因。

另外,您没有匹配的结束}

- (void) checkCollision {
    if (CGRectIntersectsRect(good.frame,bad.frame)) {


    ...

    }//for if

}//for checkCOllision

始终检查打开次数{与关闭次数}匹配。

对于此类错误,请按照以下简单步骤操作:

第一步:Cmd+A

第二步:Ctrl+I

【讨论】:

  • @H2CO3:我没有看问题只看到代码并找到了这个:p
  • @H2CO3:或者在下一分钟你会看到另一个问题说 Missing '@end' , Expected '}' and Unexpected '@' in program
  • @H2CO3:昨晚我问Code Snippet检查一下
  • 感谢Anoop的帮助
  • @user2230366:继续接受所有答案...这会将您的问题标记为已回答,您将获得一些积分..我也会获得很多积分:)
猜你喜欢
  • 2016-02-13
  • 1970-01-01
  • 1970-01-01
  • 2016-06-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-17
相关资源
最近更新 更多