[英]Objective-C protocol method invisible in Swift
我目前正在ObjC
项目中处理一些Swift类。
我的问题如下:
我在ClassA.h中声明了以下协议:
@protocol MyProtocol <NSObject>
- (void)complexMethodWithArg1:(id)arg1 arg2:(id)arg2 arg3:(id)arg3;
- (Folder *)currentDestinationFolder;
- (Flow)currentFlow;
@end
很标准的东西。
现在,我的目标是创建一个带有属性的swift类,该属性是实现此协议的对象。 很自然地,我将我的类添加到快速桥接头文件中:
//
// Use this file to import your target's public headers that you would like to expose to Swift.
//
#import "ClassA.h"
并在ClassB下的swift文件中声明我的属性,该文件是实现ANOTHER协议的UIViewController
class ClassB : UIViewController, AnotherProtocol {
var delegate:MyProtocol?
}
这里的问题是:我想在viewDidLoad
调用一堆我的委托方法。 它适用于所有其他方法,只有一种方法无法自动完成,并且如果手动输入会导致编译错误:
override func viewDidLoad() {
self.delegate?.currentDestinationFolder() // works great, no problem
self.delegate?.currentFlow() // works great, no problem
self.delegate?.complexMethodWithArg1(arg1: arg1, arg2: arg2, arg3: arg3) // PROBLEM : no autocompletion, error if entered manually !
super.viewDidLoad()
}
我不知道发生了什么,它与可选的或必需的协议方法无关,与我的委托属性是可选的(未包装)有关。
有人遇到过类似的问题吗? 似乎是某种错误?
我继续尝试在一个空项目中重现该问题。
MyProtocol.h (从您的问题和评论中获取声明)
@import Foundation;
@import UIKit;
@class CAPNavigationBar;
@protocol MyProtocol <NSObject>
- (void)setupNavigationItemInNavigationBar:(CAPNavigationBar *)navigationBar
navigationItem:(UINavigationItem *)navigationItem
inViewController:(UIViewController *)viewController;
@end
CAPNavigationBar.h (只是一个模拟)
@import Foundation;
@interface CAPNavigationBar : NSObject
@end
ViewController.swift
import UIKit
class ViewController: UIViewController {
var delegate: MyProtocol?
override func viewDidLoad() {
super.viewDidLoad()
let capNavigationBar = CAPNavigationBar()
self.delegate?.setupNavigationItemInNavigationBar(capNavigationBar, navigationItem: nil, inViewController: self)
}
}
桥接头
#import "MyProtocol.h"
#import "CAPNavigationBar.h"
摘要
一切都按预期进行。
您可能在某个地方有一个简单的错字,或者没有将所有类型正确地导入到Swift中。 尤其要确保不要将类型仅作为前向声明导入。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.