【问题标题】:Objective C protocol - some stupidly basic help needed, pleaseObjective C 协议 - 需要一些愚蠢的基本帮助,请
【发布时间】:2014-01-13 18:32:00
【问题描述】:

我在尝试添加第二个协议时遇到问题。第一个工作得很好。所以我创建了一个测试应用程序来尝试使用两种协议(因为我还在学习如何使用协议)。我不知道为什么我在理解协议时遇到这么多麻烦。我什至已经完成了教程,但仍在努力学习。

当我尝试添加第二个协议并使用它时,我的第一个问题收到以下错误:

从不兼容的类型“*const _strong”分配给“id”

但是,让我们暂时忽略它,因为我的测试应用程序在我的测试应用程序中的两种协议都给我这个错误:

找不到协议声明

因此,我将发布我的测试应用程序的代码,因为在解决更困难的问题之前,我必须了解基础知识

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

使用这些委托的测试类 Header

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

@interface IDTestingDelegates : NSObject <IDDelegateA, IDDelegateB>

@end

就在这里,我收到了两个代表的Cannot find protocol declaration 错误。我一直在搜索 SO 以及浏览教程和示例代码。 SO的最佳答案是here。但我只是不明白我做错了什么。有人可以指出我在这里缺少什么吗?

【问题讨论】:

  • 轻微拼写错误,您在其标题和实现中有IDDelegeteB,但您在测试类中引用了IDDelegateB...在继续进行之前更正所有拼写错误是个好主意。
  • 是的,我知道这一定是非常愚蠢的事情。谢谢。

标签: objective-c protocols


【解决方案1】:
@interface IDTestingDelegates : NSObject <IDDelegateA, IDDelegateB>

应该是

@interface IDTestingDelegates : NSObject <IDDelegateADelegate, IDDelegeteBDelegate>

您必须在&lt;...&gt; 中列出协议,而不是接口。

【讨论】:

  • 就在您发布此内容之前,我意识到我的错误。在拼写错误和错误的协议声明之间,我确实搞砸了应该是一个简单的测试,呵呵。
  • 哦,伙计,我在真实代码中做同样的事情。修复了它,它修复了我的第一个错误。谢谢!!!!! :-)
【解决方案2】:

@interface 声明一个类,而 ClassName &lt;X&gt; 语法要求 X 是一个协议(在您的 IDTestingDelegates 声明中)。

不知道你想在这里实现什么。

【讨论】:

  • 只是一个尝试理解协议的测试。现在,我将尝试在我的测试应用程序中使用这两种方法,看看是否可以重新创建我遇到的第一个问题。感谢您的帮助。为你+1。 :-)
  • 是的,在真实代码中修复了它,这解决了我的第一个问题。现在一切正常。再次感谢。
猜你喜欢
  • 2017-02-25
  • 1970-01-01
  • 2011-11-22
  • 1970-01-01
  • 2015-12-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多