[英]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.