在Objective-C我有the following protocol:符合目的-C协议夫特类
@protocol GCKDeviceScannerListener <NSObject>
@optional
- (void)deviceDidComeOnline:(GCKDevice *)device;
- (void)deviceDidGoOffline:(GCKDevice *)device;
- (void)deviceDidChange:(GCKDevice *)device;
@end
当试图以符合该协议在夫特的Xcode 6.1自动完成这样的:
class ViewController: UIViewController, GCKDeviceScannerListener {
override func viewDidLoad() {
super.viewDidLoad()
var deviceScanner = GCKDeviceScanner();
deviceScanner.addListener(self);
deviceScanner.startScan();
println("scanning");
}
func deviceDidComeOnline(device: GCKDevice!) {
println("deviceDidComeOnline()");
}
func deviceDidGoOffline(device: GCKDevice!) {
println("deviceDidGoOffline()");
}
func deviceDidChange(device: GCKDevice!) {
println("deviceDidChange()");
}
}
代码编译并看似在模拟器上运行正常。但是,没有一个监听器功能被触发。仅在运行Objective-C中编写的the demo project from Google时,所有工作都可以正常工作。由于最后一部分,我假设网络或硬件或类似的东西没有任何问题。
这可能是我错过了一些重要的东西,从https://developers.google.com/cast/docs/ios_sender,但我想知道如果斯威夫特代码本身是正确的,根据协议。由于该协议只有可选功能,因此很难确定它是否正确。
您可以通过使用'self.respondsToSelector测试你的ViewController的协议一致性(“deviceDidComeOnline:”)' – Darren 2014-12-07 19:30:36