1. 程式人生 > >Type '' cannot conform to protocol '' because it has requirements that cannot be satisfied

Type '' cannot conform to protocol '' because it has requirements that cannot be satisfied

jcp eth clas onf block some 編譯器 conf @class

我有一個Objective-C協議,我試圖在Swift類中實現。例如:

@class AnObjcClass;

@protocol ObjcProtocol <NSObject>
    - (void)somethingWithAnArgument:(AnObjcClass *)arg;
@end

當我嘗試在這樣的Swift類中符合它時:

@objc class SwiftClass: NSObject, ObjcProtocol {
    // ...
}

我得到以下可怕的編譯器錯誤:

Type ‘‘ cannot conform to protocol ‘‘ because it has requirements that cannot be satisfied

我該如何解決這個問題?

解決方案:

確保該協議引用的任何類都包含在橋接頭中。

當協議中使用的某個類型(協議本身,返回類型,參數類型)未包含在Swift橋接頭中時,會發生此錯誤。

由於@class AnObjcClass前向聲明,Objective-C類可以愉快地實現此協議,但似乎Swift類不能實現使用僅向前聲明的類的協議。

Type '' cannot conform to protocol '' because it has requirements that cannot be satisfied