我有这个代码块:
import Foundation
import CoreBluetooth
public class Service {
static var centralManager: CBCentralManager!
private init() {}
public static func doSomething() {
centralManager = CBCentralManager(delegate: self, queue: nil)
}
}
但是在构建时,它说我在 doSomthing() 函数的一行中出现错误表达式类型不明确,没有更多上下文
。造成这种情况的原因是什么?我怎样才能构建和运行这个小类?
最佳答案
要成为 CBCentralDelegate,您需要 a) 成为 NSObject 子类,b) 实际实现所需的协议(protocol)方法。您还应该创建一个共享实例来包装centralManager并仅公开您的界面。
public class Service: NSObject {
static let shared = Service()
private lazy var centralManager: CBCentralManager = {
return CBCentralManager(delegate: self, queue: nil)
}()
private override init() {
super.init()
}
public static func doSomething() {
//Do things with centralManager here
}
}
extension Service: CBCentralManagerDelegate {
public func centralManagerDidUpdateState(_ central: CBCentralManager) {
}
}
调用单例:
Service.shared.doSomething()
关于swift - 如果没有更多上下文,表达式类型不明确 - CoreBluetooth,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53524954/