将Swift委托分配给Objective-C对象时出错

时间:2018-10-23 10:12:46

标签: ios objective-c swift

这是我的Swift课:

class MyClass : NSObject {
    public var inAppMessagesController: MPInAppMessagesController!

fun myFunction() {
    self.inAppMessagesController.inAppInteractionDelegate = self    // Error in this line - Cannot assign value of type 'MyClass' to type 'MPInAppMessageControllerDelegate?'
}

}  

extension MyClass : MPInAppMessageControllerDelegate {
  // Functions 
}   

如评论中所述,这是错误-

  

无法将类型“ MyClass”的值分配给类型   “ MPInAppMessageControllerDelegate?”

Objective-C MPInAppMessagesController 中的

inAppInteractionDelegate

@interface MPInAppMessagesController : NSObject

@property (nonatomic, weak, nullable) id <MPInAppMessageControllerDelegate> inAppInteractionDelegate;

@end   
MPInAppMessagesController.h 中声明的

MPInAppMessageControllerDelegate

@protocol MPInAppMessageControllerDelegate<NSObject>

// Functions

@end

1 个答案:

答案 0 :(得分:0)

唯一缺少的部分是您需要将此类包含在桥接文件中

        var availableTags = [
            "ActionScript",
            "AppleScript",
            "Asp",
            "BASIC",
            "C",
            "C++",
            "Clojure",
            "COBOL",
            "ColdFusion",
            "Erlang",
            "Fortran",
            "Groovy",
            "Haskell",
            "Java",
            "JavaScript",
            "Lisp",
            "Perl",
            "PHP",
            "Python",
            "Ruby",
            "Scala",
            "Scheme"
        ];
        $("#autocomplete").autocomplete({
            source: availableTags
        });

转到此处查看SwiftObjc