类型参数不能应用于非 class 类型 'id'
Type arguments cannot be applied to non-class type 'id'
我是 ObjectiveC 的新手,已经在 Swift 工作了几年。因此,我不明白 Xcode 中以下解释的错误:
Type arguments cannot be applied to non-class type '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;
。
也尝试过:
Creating a Swift protocol with @objc
and : class
imported over the app-Bridging.h 给我相同的结果和相同的错误消息.
清理构建
清理构建文件夹(删除派生数据)
唯一起作用的是从 public 接口中删除到私有的行。这没有意义。我不想从另一个 class 设置委托并创建一个 public setter 来设置私有委托是丑陋的解决方法。
任何建议都会有所帮助。我想了解为什么会这样。我的项目中还有许多其他协议是用 ObjectiveC 编写的,它们工作正常。
ExampleProtocol
是协议,而不是 class。如果导入 header,则不需要它。如果你不导入 header,它应该是 @protocol ExampleProtocol;
我是 ObjectiveC 的新手,已经在 Swift 工作了几年。因此,我不明白 Xcode 中以下解释的错误:
Type arguments cannot be applied to non-class type '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;
。
也尝试过:
Creating a Swift protocol with
@objc
and: class
imported over the app-Bridging.h 给我相同的结果和相同的错误消息.清理构建
清理构建文件夹(删除派生数据)
唯一起作用的是从 public 接口中删除到私有的行。这没有意义。我不想从另一个 class 设置委托并创建一个 public setter 来设置私有委托是丑陋的解决方法。
任何建议都会有所帮助。我想了解为什么会这样。我的项目中还有许多其他协议是用 ObjectiveC 编写的,它们工作正常。
ExampleProtocol
是协议,而不是 class。如果导入 header,则不需要它。如果你不导入 header,它应该是 @protocol ExampleProtocol;