2009-07-03 38 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

相關問題