2009-07-03 56 views
1

我使用here中的一些代码来确定何时确定多点触摸序列中最后一根手指何时抬起。Objective C警告:从不同的Objective-C类型中传递'touchesForView:'的参数1

下面的代码:

/* 
Determining when the last finger in a multi-touch sequence has lifted 
When you want to know when the last finger in a multi-touch sequence is lifted 
from a view, compare the number of UITouch objects in the passed in set with the 
number of touches for the view maintained by the passed-in UIEvent object. 
For example: 
*/ 

- (void) touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event { 
     if ([touches count] == [[event touchesForView:self] count]) { 
      // last finger has lifted.... 
     } 
    } 

我得到警告:

passing argument 1 of 'touchesForView:' from distinct Objective-C type

的代码生成并运行很好,但我想删除它但不明白警告的含义。有任何想法吗?

回答

3

当您提供与预期类型不同的对象时,会出现特定的警告。

在这种情况下,touchesForView:需要一个UIView对象,但你传递给它的任何类型self恰好是在这个代码的对象。

为了使报警消失,你可以传递正确类型的对象,或者您可以强制编译器将self指针转换为正确的类型:

if ([touches count] == [[event touchesForView:(UIView *)self] count]) 

被警告,但是,如果self的行为不像UIView,那么您可以为自己设定一些微妙的错误。

更新:

我做了快速搜索,发现this article,这对处理可可警告,一些优秀的指导方针,以及它们的常见原因。

基于这些信息,我想快速地列出您发布的代码应该发生的情况。我假设您使用Xcode中的模板创建了一个新的iPhone应用程序,并且该应用程序有一个UIView(如Interface Builder中所示)。

要使用您发布的代码,你将创建一个自定义UIView子如下:

// YourViewClass.h: 
@interface YourViewClass : UIView // Note #1 
{ 
} 
@end 

// YourViewClass.m: 

@import "YourViewClass.h" // Note #2 

@implementation YourViewClass 

- (void) touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event 
{ 
    if ([touches count] == [[event touchesForView:self] count]) 
    { 
     // last finger has lifted.... 
    } 
} 

@end 

而且在Interface Builder中,您将设置view对象的类型到YourViewClass,然后你应该很好走。

随着代码如上所示,你不应该得到那个警告。这导致我认为上述其中一个步骤没有做好。首先,可以肯定的是:

  1. self对象实际上是一个UIView子类(注#1)
  2. #import在源文件中的标题为您的类(注2)
+0

非常感谢你的时间,链接和回应!我其实并没有创建一个新的iPhone应用程序,而是在cocos2d(一个游戏引擎)的土地上,但我认为下一个遇到类似问题并阅读此答案的人将会节省很多时间:-)再次感谢! – Stu 2009-07-03 14:57:55

相关问题