【问题标题】:Type arguments cannot be applied to non-class type 'id'类型参数不能应用于非类类型“id”
【发布时间】:2017-12-26 09:58:54
【问题描述】:

我是 ObjectiveC 的新手,并且已经在 Swift 中工作了几年。因此,我不明白 Xcode 中以下解释的错误:

类型参数不能应用于非类类型'id'

我的协议:

@protocol ExampleProtocol<NSObject>
@required
        
-(NSString *)title;
-(NSString *)album;
    
@end

我在 MyService.h 文件中的实现:

@interface MyService : NSObject 
@property (nonatomic, assign, readwrite) id<ExampleProtocol> delegate;
@end

错误发生在以下行:

> @property (nonatomic, assign, readwrite) id<ExampleProtocol> delegate;

另外:

  • 我已导入所需的文件 .h,其中 ExampleProtocol 代码位于 MyService.m 文件中
  • 在顶部的 MyService.h 文件中添加了@class ExampleProtocol;

也试过了:

  • 使用通过 app-Bridging.h 导入的 @objc: class 创建 Swift 协议会得到相同的结果和相同的错误消息。

  • 干净的构建

  • 清理构建文件夹(删除派生数据)

    唯一起作用的是将公共接口中的行删除到私有接口。这没有意义。我不想从另一个类设置委托并创建一个设置私有委托的公共设置器是丑陋的解决方法。

任何建议都会有所帮助。我想了解为什么会发生这种情况。在我的项目中,还有很多其他用 ObjectiveC 编写的协议都可以正常工作。

【问题讨论】:

  • 你试过没有@class ExampleProtocol;吗? ExampleProtocol 是一个协议,而不是一个类。导入header就不需要了,不导入header应该是@protocol ExampleProtocol;
  • 您的解决方案是正确的答案。请将其发布为答案,以便我接受并投票。

标签: objective-c protocols xcode9.2


【解决方案1】:

ExampleProtocol 是一个协议,而不是一个类。如果您导入标头,则不需要它。如果不导入header,应该是@protocol ExampleProtocol;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-09
    • 2022-11-11
    • 2015-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-19
    相关资源
    最近更新 更多