2014-01-13 56 views
0

我在嘗試添加第二個協議時出現問題。第一個工作正常。所以我創建了一個測試應用程序來嘗試使用兩種協議(因爲我仍然在學習如何使用協議)。我不知道爲什麼我在理解協議時遇到了很多麻煩。我甚至通過教程,仍然與他們鬥爭。Objective C協議 - 需要一些愚蠢的基本幫助,請

我的第一個問題,當我試圖添加第二個協議,並使用它,我收到以下錯誤:

Assigning to ‘id’ from incompatible type ‘ *const _strong'

但是,讓我們忽略現在,因爲我的測試應用程序是給我這個錯誤在我的測試應用程序這兩個協議:

Cannot find protocol declaration

所以,我會張貼我的測試應用程序的代碼,因爲我必須解決更多迪之前瞭解的基礎知識困難的問題。

DelegateA部首

#import <Foundation/Foundation.h> 

@protocol IDDelegateADelegate <NSObject> 
@end 

@interface IDDelegateA : NSObject 
//other properties here 
@property (nonatomic, assign) id<IDDelegateADelegate> delegateA; 
@end 

DelegateA實施

#import "IDDelegateA.h" 

@implementation IDDelegateA 
@synthesize delegateA; 
//other methods and properties go here 
@end 

DelegateB部首

#import <Foundation/Foundation.h> 

@protocol IDDelegeteBDelegate <NSObject> 
@end 

@interface IDDelegeteB : NSObject 
//other properties here 
@property (nonatomic, assign) id<IDDelegeteBDelegate> delegateB; 
@end 

DelegateB實施

#import "IDDelegeteB.h" 

@implementation IDDelegeteB 
@synthesize delegateB; 
//other methods and properties go here 
@end 

使用這些代表

#import <Foundation/Foundation.h> 
#import "IDDelegateA.h" 
#import "IDDelegeteB.h" 

@interface IDTestingDelegates : NSObject <IDDelegateA, IDDelegateB> 

@end 

就在這裏我收到Cannot find protocol declaration錯誤都代表測試類的頭。我一直在搜索SO以及通過教程和示例代碼。 SO的最佳答案是here。但是我只是沒有明白我做錯了什麼。有人可以指出我在這裏失蹤了什麼嗎?

+2

小寫拼寫錯誤,您在其頭文件和實現中有'IDDelegeteB',但是您在測試類中引用'IDDelegateB' ...在進一步繼續之前更正所有拼寫錯誤是個好主意。 – abiessu

+0

是的,我知道它必須是非常愚蠢的東西。謝謝。 – Patricia

回答

2
@interface IDTestingDelegates : NSObject <IDDelegateA, IDDelegateB> 

應該

@interface IDTestingDelegates : NSObject <IDDelegateADelegate, IDDelegeteBDelegate> 

你必須列出協議<...>,沒有接口。

+0

就在你發佈這個之前,我意識到了我的錯誤。在拼寫錯誤和錯誤的協議聲明之間,我肯定會弄糟本應該是一個簡單的測試,呵呵。 – Patricia

+0

噢,夥計,我在真正的代碼中做了同樣的事情。修復了這個問題,它修復了我的第一個錯謝謝!!!!! :-) – Patricia

1

@interface聲明一個類,而ClassName <X>語法期望X是一個協議(在您的聲明IDTestingDelegates中)。

不確定你在這裏試圖達到什麼。

+0

只是一個試圖瞭解協議。現在,我將嘗試在我的測試應用中使用這兩種方法,並查看是否可以重新創建我遇到的第一個問題。感謝您的幫助。爲你+1。 :-) – Patricia

+0

是的,修復它在真正的代碼,並解決了我的第一個問題。現在一切正常。再次感謝。 – Patricia