【问题标题】:Unable to access protocol methods via delegate无法通过委托访问协议方法
【发布时间】:2018-10-23 17:40:48
【问题描述】:

通过委托访问协议方法时,出现以下错误: "没有已知的选择器 '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'

【问题讨论】:

  • 请在主持人出现并完全关闭您之前立即删除您的其他身份stackoverflow.com/users/10355502/vish-desh
  • "调用协议方法时出错" 是的,但您需要显示执行该调用的上下文。大概它位于 SStreamManager 的 .m 文件中。该文件需要#import Swift 生成的标头,正如您在其他问题的评论中已经告知的那样。
  • 我遇到了同样的问题,尝试删除 Xcode 的 DerivedData(确保您的 xcode 已关闭,删除后打开 xcode)。如果有帮助,请告诉我。路径:~/Library/Developer/Xcode/DerivedData
  • 我删除了@matt。
  • @Koen 请回滚。语言对问题至关重要

标签: objective-c swift protocols


【解决方案1】:

这变得很荒谬,因为您一直拒绝显示任何真实代码。所以我会向展示一些真实的代码。以下是 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

编译。去吧,照样做。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-21
    • 1970-01-01
    • 1970-01-01
    • 2018-01-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多