无法通过委托访问协议方法
Unable to access protocol methods via delegate
通过委托访问协议方法时出现以下错误:
"No known instance method for selector 'lostConnection'"
Swift 协议:
@objc protocol GameDelegate {
func lostConnection()
}
Objective C游戏文件
//game.h
@protocol GameDelegate;
@interface SSStreamManager : NSObject
@property (assign) id<GameDelegate> delegate
@end
调用协议方法时出错
[self.delegate lostConnection]; // No known instance method for selector 'lostConnection'
您没有显示任何实际代码,但这里有一个示例可以帮助您入门。这些是 iOS 应用程序项目中的三个文件:
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
即编译。您应该能够在自己的代码中遵循此模型。
通过委托访问协议方法时出现以下错误: "No known instance method for selector 'lostConnection'"
Swift 协议:
@objc protocol GameDelegate {
func lostConnection()
}
Objective C游戏文件
//game.h
@protocol GameDelegate;
@interface SSStreamManager : NSObject
@property (assign) id<GameDelegate> delegate
@end
调用协议方法时出错
[self.delegate lostConnection]; // No known instance method for selector 'lostConnection'
您没有显示任何实际代码,但这里有一个示例可以帮助您入门。这些是 iOS 应用程序项目中的三个文件:
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
即编译。您应该能够在自己的代码中遵循此模型。