objective-c - 无法通过委托访问协议方法
问题描述
通过委托访问协议方法时出现以下错误:“选择器'lostConnection'没有已知的实例方法”
斯威夫特协议:
@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 {
}
东西.h
#import <Foundation/Foundation.h>
@protocol GameDelegate;
@interface Thing : NSObject
@property (assign) id<GameDelegate> delegate;
@end
东西.m
#import "Thing.h"
#import "MyApp-Swift.h"
@implementation Thing
- (void) test {
[self.delegate lostConnection];
}
@end
编译。去吧,照样做。
推荐阅读
- java - 如何从 List 中获取元素
元素=新的 ArrayList<>();? - html - 如何将图像直接堆叠在 div 之上?
- json - 在 Angular 中难以实现分页功能
- css - Angular cdk拖放:拖放到其他容器后保持元素样式
- python - 如何在 Pytest 的收集时添加自定义 XML 属性?
- mysql - Delete rows to keep only top values in a sql table
- react-native - onEndReached 在数据加载完成之前触发
- ios - 在不覆盖自定义更改的情况下更新 GitHub iOS SDK
- unity3d - 如何让鼠标点击对象,而不是当它在被点击的按钮后面时?
- gatsby - gatsby-starter-default 启动器中的 Image 组件的用途是什么?