【问题标题】:No type or protocol named 'ComposeDelegate' error after adding bridging header添加桥接头后没有名为“ComposeDelegate”的类型或协议错误
【发布时间】:2020-11-03 01:11:09
【问题描述】:

XCode 12.1

此项目目标在添加伞头后出现编译器错误add swift functionality to an existing objective c target

现在所有目标现有的各种<ComposeDelegate> 声明都出现No type or protocol named 'ComposeDelegate' 错误。

ComposeViewController.h

@class ComposeViewController;
@class DataModel;
@class Message;

// The delegate protocol for the Compose screen
@protocol ComposeDelegate <NSObject>
- (void)didSaveMessage:(Message*)message atIndex:(int)index;
@end

// The Compose screen lets the user write a new message
@interface ComposeViewController : UIViewController /*<UITextViewDelegate>*/

@property (nonatomic, assign) id<ComposeDelegate> delegate;
@property (nonatomic, strong) DataModel* dataModel;
@property (nonatomic, strong) AFHTTPClient *client;
@end

其他类似的代码错误也会出现在目标代码的其他地方。

如何解决这个问题?

【问题讨论】:

  • 警告和错误弹出,因为它已经在其他地方定义了。预编译器会忽略声明,因此会弹出更多错误。猜测是@class ComposeDelegate;,这有点过头了。
  • 提示:一般来说,当您收到警告或错误时 - 不足为奇 - 错误/错误必须高于代码或项目设置中的警告。因此,在搜索问题时,您始终可以使用///* ... */ 来注释某些行并查看更改。
  • 提示 2:/*&lt;UITextViewDelegate&gt;*/ 在直接跟在 ClassName 之后是“危险的”。在注释掉的代码之前直接换行。
  • 这实际上取决于在哪里、如何以及什么样的桥接头。假设 Objc 桥接到 Swift。除了项目设置之外,您不会在任何地方重新导入标题。否则,您将创建一个乒乓球游戏。桥接标头当然可以包含其他桥接标头,但到目前为止我记得每个项目都使用一个标头。

标签: ios objective-c swift xcode


【解决方案1】:

为您的协议创建一个文件并将协议声明复制到该文件中。

// ComposeDelegate.h

#idndef ComposeDelegate_h
#define ComposeDelegate_h

// The delegate protocol for the Compose screen
@protocol ComposeDelegate <NSObject>
- (void)didSaveMessage:(Message*)message atIndex:(int)index;
@end

#endif

然后使用此文件包含您想要的任何位置。 虽然创建一个额外的文件似乎是额外的工作,但它可以让您更好地控制源代码的预处理。

#ifndef SomeName_h ... #endif 防止代码被执行两次。

PS:如果您编写 @interface 并且声明的 Class 未在之前/以上使用,则您不必也不应该预先声明 ClassName (@class ComposeDelegate;) 如果不是真的需要。

【讨论】:

  • 我尝试创建一个名为 ComposeDelegate.h 的新文件。将协议放入其中并将其导入有错误的文件中。仍然出现错误。还是一头雾水。
猜你喜欢
  • 2016-04-08
  • 2016-11-05
  • 2020-12-30
  • 1970-01-01
  • 2022-12-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多