Search code examples
objective-cswiftprotocols

Unable to access protocol methods via delegate


While accessing protocol methods via delegate I'm getting following error: "No known instance method for selector 'lostConnection'"

Swift Protocol:

@objc protocol GameDelegate {
    func lostConnection()
}

Objective C game file

//game.h

@protocol GameDelegate;
@interface SSStreamManager : NSObject 

@property (assign) id<GameDelegate> delegate

@end

Getting error while calling protocol methods

[self.delegate lostConnection]; // No known instance method for selector 'lostConnection'

Solution

  • You haven't shown any real code, but here's an example that will get you started. These are the three files in an iOS app project:

    ViewController.swift

    import UIKit
    @objc protocol GameDelegate {
        func lostConnection()
    }
    class ViewController: UIViewController {
    }
    

    Thing.h

    #import <Foundation/Foundation.h>
    @protocol GameDelegate;
    @interface Thing : NSObject
    @property (assign) id<GameDelegate> delegate;
    @end
    

    Thing.m

    #import "Thing.h"
    #import "MyApp-Swift.h"
    @implementation Thing
    - (void) test {
        [self.delegate lostConnection];
    }
    @end
    

    That compiles. You should be able to follow this model in your own code.