gpt4 book ai didi

ios - 尽管桥接头工作正常,但无法在 Swift 中实例化 Obj C 类

转载 作者:可可西里 更新时间:2023-11-01 02:17:05 25 4
gpt4 key购买 nike

我正在努力将单个 objective-c 类引入 swift 中。我的桥接 header 工作得很好——Xcode 甚至在我键入它时建议 obj c 类的名称。我什至设法从我的 swift 文件中引用了同一个头文件中的委托(delegate)协议(protocol):

BrowserViewController.swift

import UIKit

class BrowserViewController: UIViewController, BrowserTabViewDelegate {

@IBOutlet weak var browserView: UIView!
@IBOutlet weak var webViewContainer: UIView!


override func viewDidLoad() {
super.viewDidLoad()

let tabview: BrowserTabView = BrowserTabView();
// Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

}

Sreen cap of the error: Use of unresolved identifier 'BrowserTabView'.请注意,BrowserTabViewDelegate 在被引用时没有任何问题。它来自与 BrowserTabView 相同的 obj c header 。它建议类名,但我不能对其调用 init。

BrowserTabView.h

#import <UIKit/UIKit.h>
@class BrowserTabView;

@protocol BrowserTabViewDelegate <NSObject>

@optional

-(void)BrowserTabView:(BrowserTabView *)browserTabView didSelectTabAtIndex:(NSUInteger)index;
-(void)BrowserTabView:(BrowserTabView *)browserTabView willRemoveTabAtIndex:(NSUInteger)index;
-(void)BrowserTabView:(BrowserTabView *)browserTabView didRemoveTabAtIndex:(NSUInteger)index;
-(void)BrowserTabView:(BrowserTabView *)browserTabView exchangeTabAtIndex:(NSUInteger)index withTabAtIndex:(NSUInteger)toIndex;
-(BOOL)BrowserTabView:(BrowserTabView *)tabView shouldChangeTitle: (NSString*)title;

@end

@interface BrowserTabView : UIView <UIGestureRecognizerDelegate, UITextFieldDelegate>

@property (nonatomic, strong) UIImage *backgroundImage;
@property (nonatomic, readonly) NSUInteger numberOfTabs;
@property (nonatomic, readonly) NSInteger selectedTabIndex;

@property (nonatomic, readonly) NSMutableArray *reuseQueue;
@property (nonatomic, weak) id<BrowserTabViewDelegate> delegate;

-(id)initwithTabTitles:(NSArray *)titles andDelegate:(id)del;
-(void)addTabWithTitle:(NSString *)title;
-(void)setTabTitlesWithArray:(NSArray *)titles;
-(void)setSelectedTabIndex:(NSInteger)selectedTabIndex animated:(BOOL)animation;
-(void)removeTabAtIndex:(NSInteger)index animated:(BOOL)animated;
- (void)handleLongPress:(UILongPressGestureRecognizer *)gr;
- (void)handlePanGesture:(UIPanGestureRecognizer *)sender;


@end

这是我第一次在这样的问题上被困这么久......我见过的大多数现有问题都与不良的桥接头有关 - 但我很确定我的问题很好.如果我可以解决我的问题,请告诉我,在此先感谢您的帮助。

WI Vision-Bridging-Header.h

//
// Use this file to import your target's public headers that you would like to expose to Swift.
#import "BrowserTabView.h"
#import "BrowserTab.h"

最佳答案

真是个好问题。这看起来像是一个错误,我们可以将其记录到 Swift 的社区。按照以下方式更改委托(delegate)将帮助您:

@protocol BrowserTabViewDelegate <NSObject>

@optional

-(void)browserTabView:(BrowserTabView *)browserTabView didSelectTabAtIndex:(NSUInteger)index;
-(void)browserTabView:(BrowserTabView *)browserTabView willRemoveTabAtIndex:(NSUInteger)index;
-(void)browserTabView:(BrowserTabView *)browserTabView didRemoveTabAtIndex:(NSUInteger)index;
-(void)browserTabView:(BrowserTabView *)browserTabView exchangeTabAtIndex:(NSUInteger)index withTabAtIndex:(NSUInteger)toIndex;
-(BOOL)browserTabView:(BrowserTabView *)tabView shouldChangeTitle: (NSString*)title;

@end

在方法名称中应该使用 browserTabView 而不是 BrowserTabView

关于ios - 尽管桥接头工作正常,但无法在 Swift 中实例化 Obj C 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36752242/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com