【问题标题】:Can not find Swift Protocol declaration in Obj-C class在 Obj-C 类中找不到 Swift 协议声明
【发布时间】:2017-08-29 05:38:40
【问题描述】:

我在Swift 中的Class 上创建了我在Obj-C 启用的项目中使用的那个类及其协议,但是我在编译我的项目时遇到了错误。

找不到“SpeechRecognizerDelegate”的协议声明;做过 你的意思是“SFSpeechRecognizerDelegate”?

谁能指导我如何在我的 Obj-C 类中使用 Swift 类协议。

这是我的 Swift 代码:

protocol SpeechRecognizerDelegate : class  {
    func speechRecognitionFinished(_ transcription:String)
    func speechRecognitionError(_ error:Error)
}


class SpeechRecognizer: NSObject, SFSpeechRecognizerDelegate {
    open weak var delegate: SpeechRecognizerDelegate?

}

Obj-C 中的协议使用:

#import "ARBot-Swift.h"

@interface ChatScreenViewController : JSQMessagesViewController <SpeechRecognizerDelegate>

如果需要更多信息,请告诉我。

提前致谢。

【问题讨论】:

    标签: objective-c swift3 swift-protocols


    【解决方案1】:

    在 Swift 中:

    @objc public protocol YOURSwiftDelegate {
        func viewReceiptPhoto()
        func amountPicked(selected: Int)
    }
    
    class YourClass: NSObject {
        weak var delegat: YOURSwiftDelegate?
    }
    

    在 Objective-C 的 headerFile.h 中

    @protocol YOURSwiftDelegate;
    
    @interface YOURController : UIViewController < YOURSwiftDelegate >
    

    在 Objective-C Implementation.m

    SwiftObject * swiftObject = [SwiftObject alloc] init];
    swiftObject.delegate = self
    

    【讨论】:

    • 这是正确答案。关键是在您的目标 C 代码中添加 @protocol YOURSwiftDelegate; 行。之后,下一个错误将要求您定义缺少的函数。
    【解决方案2】:

    在你的 Swift 文件中像这样定义你的 Swift 协议。

    @objc protocol SpeechRecognizerDelegate: class{
      func speechRecognitionFinished(_ transcription:String)
      func speechRecognitionError(_ error:Error)
    }
    

    在您的项目设置中创建一个 Swift 模块,然后使用它。您可以找到 here complete blog 进行混合语言编码。

    然后在Objective C类中使用Protocol,

    我们需要在 Objective C 文件中添加 protocol -

    #import "ARBot-Swift.h"
    
    @interface ChatScreenViewController : JSQMessagesViewController <SpeechRecognizerDelegate>
    

    那你需要符合protocol方法-

    - (void)viewDidLoad {
        [super viewDidLoad];
        SpeechRecognizer * speechRecognizer = [[SpeechRecognizer alloc] init];
        speechRecognizer.delegate = self;
    }
    
    
    #pragma mark - Delegate Methods
    -(void)speechRecognitionFinished:(NSString *) transcription{
       //Do something here
    }
    
    -(void)speechRecognitionError:(NSError *) error{
       //Do something here
    }
    

    【讨论】:

    • 在里面添加协议是什么意思?我对确认协议做同样的事情,但得到编译器错误。
    • 您需要添加协议方法来进行协议确认。你做过吗? @CodeChanger
    • @objc protocol SpeechRecognizerDelegate你是这样使用你的协议的吗?
    【解决方案3】:

    @objc 属性添加到您的协议:

    @objc protocol SpeechRecognizerDelegate : class  {
        //...
    }
    

    【讨论】:

      【解决方案4】:

      在关注(协议上的导入标头 + Objc 注释)之后我遇到了类似的问题。在使用来自 Objective C 标头的 Swift 代码时,我收到了警告。仅通过导入实现 .m 文件即可解决。

      【讨论】:

        【解决方案5】:

        使用前向声明在 Objective-C 头文件中包含 Swift 类

        //MySwiftClass.swift
        @objc protocol MySwiftProtocol {}
        @objcMembers class MySwiftClass {}
        
        // MyObjcClass.h
        @class MySwiftClass;
        @protocol MySwiftProtocol;
        
        @interface MyObjcClass : NSObject
        - (MySwiftClass *)returnSwiftClassInstance;
        - (id <MySwiftProtocol>)returnInstanceAdoptingSwiftProtocol;
        // ...
        @end
        

        【讨论】:

          猜你喜欢
          • 2015-12-18
          • 1970-01-01
          • 2011-09-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多