我有一个Objective-C协议,主要用于Objective-C对象和一个或两个Swift对象.
我想在Swift中扩展协议并添加2个函数.一个用于注册通知,另一个用于处理通知.
如果我添加这些
func registerForPresetLoadedNotification() {
NSNotificationCenter.defaultCenter().addObserver(self as AnyObject,
selector: #selector(presetLoaded(_:)),
name: kPresetLoadedNotificationName,
object: nil)
}
func presetLoaded(notification: NSNotification) {
}
Run Code Online (Sandbox Code Playgroud)
我在#selector上收到错误 Argument of '#selector' refers to a method that is not exposed to Objective-C
如果我然后标记presetLoaded,因为@objc我得到一个错误@objc can only be used with members of classes, @objc protocols, and concrete extensions of classes
我也无法将协议扩展标记为@objc
当我将Objective-C协议创建为Swift协议时,我得到了同样的错误.
有没有办法实现这个适用于使用该协议的Objective-C和Swift类?
我正在开发具有多个 ViewController 的 BLE iOS (Swift) 应用程序。主 ViewController 有一个按钮,可导航到 TableViewController,该按钮已检测到要连接的 BLE 设备。但是当我返回主视图或其他视图时,外围设备断开连接。我试图将外围设备从 TableViewController 传递到主 ViewController,但它仍然断开连接。
主视图控制器:
var bleManager: BLEManager!
var peripheral: CBPeripheral!
override func viewDidLoad() {
bleManager = BLEManager()
super.viewDidLoad()
}
override func viewWillAppear(_ animated: Bool) {
if let peripheral = self.peripheral {
do {
print("Value from display = \(peripheral.state)")
}
}
}
func setPeripheral(sent: CBPeripheral) {
self.peripheral = sent
}
@IBAction func manageDevice(sender: UIButton)
{
// 1. Instantiate TableViewController
let tableViewController = self.storyboard?.instantiateViewController(withIdentifier: "TableViewController") as! TableViewController
// 2. Set …Run Code Online (Sandbox Code Playgroud)