代码之家  ›  专栏  ›  技术社区  ›  CodeChanger

在Obj-C类中找不到Swift协议声明

  •  6
  • CodeChanger  · 技术社区  · 7 年前

    我已在上创建 Class Swift 我正在使用的那个类及其协议 Obj-C 已启用项目,但编译项目时出现以下错误。

    找不到“SpeechRecognizerDelegate”的协议声明;做 你是说“sfspeechrecognizerregate”?

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

    protocol SpeechRecognizerDelegate : class  {
        func speechRecognitionFinished(_ transcription:String)
        func speechRecognitionError(_ error:Error)
    }
    
    
    class SpeechRecognizer: NSObject, SFSpeechRecognizerDelegate {
        open weak var delegate: SpeechRecognizerDelegate?
    
    }
    

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

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

    5 回复  |  直到 5 年前
        1
  •  9
  •   Shahriyar    5 年前

    @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 >
    

    SwiftObject * swiftObject = [SwiftObject alloc] init];
    swiftObject.delegate = self
    
        2
  •  4
  •   Community CDub    4 年前

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

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

    在项目设置中创建Swift模块,然后使用它。你可以找到 here complete blog

    协议 在…内 文件-

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

    协议 方法-

    - (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
    }
    
        3
  •  2
  •   StackRunner    7 年前

        4
  •  1
  •   juhan_h    7 年前

    添加 @objc

    @objc protocol SpeechRecognizerDelegate : class  {
        //...
    }
    
        5
  •  0
  •   VyacheslavBakinkskiy    3 年前

    使用转发声明在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