2012-07-10 23 views
-1

我的問題是關於一種情況下,對於只有一個參數但不是兩種情況的方法,它可以正常工作。該代碼來自Apple動畫示例代碼。當使用多個參數時找不到實例方法

主視圖對象(self)只讀引用了抽象超類對象s。

它可以調用從這樣的超類繼承的實例方法:

[self.s myMethod:param1]; 

,它工作正常。

然而,當我嘗試:

[self.s anotherMethod:param1 secondParam:param2]; 

我得到的編譯錯誤:

Semantic Issue > Instance method '-anotherMethod:secondParam:' not found (return type defaults to 'id')

這兩種方法都必須返回類型 - (無效)。

對於第一種情況和第二種情況,一切都以相同的方式進行聲明和定義。

任何建議將不勝感激。

(我真的不希望訴諸參數捆綁成一個單一的對象!)


修正與例如超類的.h文件下面

聲明加入這樣和繼承類

爲超級類:

@interface SuperClass : NSObject 
{ 
... 
} 
... 
- (void) myMethod:(SomeObject *) param1; 
- (void) anotherMethod:(SomeObject *) param1: (int) param2; 
@end 

˚F或繼承的類:在每個類

的.m文件

#import "SuperClass.h" 
@interface InheritedClass : SuperClass 
{ 
... 
} 
... 
- (void) myMethod:(SomeObject *) param1; 
- (void) anotherMethod:(SomeObject *) param1: (int) param2; 
@end 

實現已經嘗試添加這對繼承的類.m文件。

@interface InheritedClass() 
- (void) anotherMethod:(SomeObject *) param1: (int) param2; 
@end 
+1

在s'類的'@interface'中是否存在'-anotherMethod:secondParam:'的聲明? – 2012-07-10 02:51:37

+1

不知道這裏有什麼問題。看起來你理解這個問題,但你對它(*)不滿*。所以是的 - 如果一個方法需要一個參數,而您提供了兩個參數,則會引發錯誤。你究竟想要做什麼? – 2012-07-10 02:55:21

+2

你可以發佈兩種方法的@interface聲明嗎?它們是否都在頭文件中? – Stew 2012-07-10 03:10:28

回答

1

這一呼籲:

[self.s anotherMethod:param1 secondParam:param2]; 

...表示方法名是anotherMethod:secondParam:

此聲明:

- (void) anotherMethod:(SomeObject *) param1: (int) param2; 

...不具有相同的名稱。

難道你的意思是說:

- (void) anotherMethod:(SomeObject *)param1 secondParam:(int)param2; 

+0

是的,就是這樣。非常感謝,我非常感謝。 – 2012-07-10 06:00:54