【发布时间】:2021-12-30 02:55:48
【问题描述】:
在 Swift 中尝试使用 ObjC 中的代码时,我遇到了以下 Swift 错误:
'myClass & myProtocol' cannot be used as a type conforming to protocol 'myProtocol' because 'myProtocol' has static requirements
代码大致如下:
@interface FooGenericContainer <T : MyClass < MyProtocol > *> : MyClass
func asFoo() -> FooGenericContainer<MyClass & MyProtocol> { /// <-- error here
....
}
@protocol MyProtocol <NSObject, NSCopying, NSMutableCopying, AnotherProtocol>
@end
...
@protocol AnotherProtocol <NSObject, NSCopying, NSMutableCopying>
+ (NSDictionary *)method1;
@optional
+ (NSDictionary *)method2;
@end
什么是协议的“静态要求”,这些是它具有的类方法吗?
我该如何克服这个错误?
我可以返回FooGenericContainer 而不指定泛型类型吗?
【问题讨论】:
-
myProtocol长什么样子? -
@Sweeper 更新问题。
-
嗯,
method1和method2都是静态要求。 -
请参阅here 以获得解释。
-
@Biclops 更新了问题。很抱歉造成混乱,复制粘贴的真实代码并忘记将其翻译成示例。
标签: swift objective-c