在带有objective-c-header文件的swift项目中使用objective-c代码时,我在执行命令时遇到问题。
错误是编译时说:
Cannot invoke 'registerObserver' with an argument list of type '(ViewController)'
我使用的是基于BluetoothManager.framework(私有框架)构建的MDBluetothManager库。
registerObserver方法声明如下:
MDBluetoothManager.m
#pragma mark - Observer methods
- (void)registerObserver:(id<MDBluetoothObserverProtocol>)observer
{
[self.observers addObject:observer];
}
- (void)unregisterObserver:(id<MDBluetoothObserverProtocol>)observer
{
[self.observers removeObject:observer];
}
MDBluetoothManager.h
- (void)registerObserver:(id<MDBluetoothObserverProtocol>)observer;
- (void)unregisterObserver:(id<MDBluetoothObserverProtocol>)observer;
MD蓝牙观察员.h
@protocol MDBluetoothObserverProtocol <NSObject>
@required
- (void)receivedBluetoothNotification:
(MDBluetoothNotification)bluetoothNotification;
在我的objective-c项目中,我正在执行以下操作:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[[MDBluetoothManager sharedInstance] registerObserver:self];
[self.bluetoothSwitch setOn:[[MDBluetoothManager sharedInstance] bluetoothIsPowered]];
}
但当我在Swift中做同样的事情时,它不起作用:
override func viewDidLoad() {
super.viewDidLoad()
MDBluetoothManager.sharedInstance().registerObserver(self)
}
我会错过什么?
包装类(MDBluetoothManager文件)的链接可以在我在github上找到的一个名为BeeTee的示例项目中找到。https://github.com/michaeldorner/BeeTee/tree/master/BeeTee
我在@Sergii Martynenko JR.的帮助下解决了我的问题
我用MDBluetoothObserver协议扩展了我的类:
class ViewController: UIViewController, MDBluetoothObserverProtocol {
然后我添加了必要的功能:
func receivedBluetoothNotification(bluetoothNotification: MDBluetoothNotification) {
...
}
然后我可以注册:
MDBluetoothManager.sharedInstance().registerObserver(self)
现在,在一切正常之前,我还有其他问题,但那是另一个问题。感谢所有的帮助。