首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >目标C警告:从不同的目标-C类型传递“touchesForView:”的参数1

目标C警告:从不同的目标-C类型传递“touchesForView:”的参数1
EN

Stack Overflow用户
提问于 2009-07-03 13:33:33
回答 1查看 4.7K关注 0票数 1

我正在使用来自这里的一些代码来确定何时多点触摸序列中的最后一个手指已经解除。

下面是代码:

代码语言:javascript
复制
/*
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

代码构建并运行良好,但我想删除它,但不明白警告意味着什么。有什么想法吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2009-07-03 13:46:28

当您提供一个与预期不同的类型的对象时,就会出现这个特别的警告。

在这种情况下,touchesForView:需要一个UIView对象,但是您要传递给它一个无论self类型在此代码中是什么类型的对象。

为了消除警告,可以传递正确类型的对象,也可以强制编译器将self指针转换为正确的类型:

代码语言:javascript
复制
if ([touches count] == [[event touchesForView:(UIView *)self] count])

但是,请注意,如果self的行为不像UIView,那么您可能会为自己设置一些细微的错误。

更新:

我做了一个快速搜索,发现了这篇文章,它有一些很好的指南来处理可可警告,以及它们的共同原因。

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

要使用您发布的代码,您需要创建一个定制的UIView子类,如下所示:

代码语言:javascript
复制
// YourViewClass.h:
@interface YourViewClass : UIView // Note #1
{
}
@end

代码语言:javascript
复制
// 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中,您可以将view对象的类型设置为YourViewClass,然后就可以了。

有了我前面所示的代码,您就不应该收到警告。这使我认为上述步骤中的一个没有得到适当的执行。首先,要确保:

  1. 您的self对象实际上是一个UIView子类(注1)
  2. 您可以在源文件中#import类的头(注2)
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1079380

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档