无法在 Swift 中实例化 Obj C Class 尽管工作桥接 header
Can't Instantiate Obj C Class in Swift in spite of working bridging header
我正在努力将单个 objective c class 拉入 swift。我的桥接 header 工作得很好 - Xcode 甚至在我键入它时建议 obj c class 的名称。我什至设法在我的 swift 文件中的同一个 header 文件中引用了委托协议:
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。它建议使用 class 名称,但我无法对其调用 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
这是我第一次在这样的问题上被困这么久......我看到的大多数现有问题都与不良桥接有关 headers- 但我很确定我的很好。让我知道是否可以解决我的问题,在此先感谢您的帮助。
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 的社区。按照以下方式更改委托将对您有所帮助:
@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。
我正在努力将单个 objective c class 拉入 swift。我的桥接 header 工作得很好 - Xcode 甚至在我键入它时建议 obj c class 的名称。我什至设法在我的 swift 文件中的同一个 header 文件中引用了委托协议:
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。它建议使用 class 名称,但我无法对其调用 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
这是我第一次在这样的问题上被困这么久......我看到的大多数现有问题都与不良桥接有关 headers- 但我很确定我的很好。让我知道是否可以解决我的问题,在此先感谢您的帮助。
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 的社区。按照以下方式更改委托将对您有所帮助:
@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。