我尝试在我正在开发的OS X应用程序中使用Core Bluetooth框架,这实际上将Mac连接到您的iphone以通过Bluetooth LE执行不同的操作。
问题是,当我简单地初始化CBCentralManager及其委托方法时,我得到一个奇怪的错误:
Undefined symbols for architecture x86_64:
"_CBAdvertisementDataLocalNameKey", referenced from:
__TFC5test311AppDelegate14centralManagerfS0_FTGSQCSo16CBCentralManager_21didDiscoverPeripheralGSQCSo12CBPeripheral_17advertisementDataGSQGVSs10DictionaryCSo8NSObjectPSs9AnyObject___4RSSIGSQCSo8NSNumber__T_ in AppDelegate.o
"_OBJC_CLASS_$_CBCentralManager", referenced from:
__TMaCSo16CBCentralManager in AppDelegate.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
这是我的代码:
import Cocoa
import CoreBluetooth
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate, CBCentralManagerDelegate {
@IBOutlet weak var window: NSWindow!
var centralManager: CBCentralManager!
func centralManager(central: CBCentralManager!, didDiscoverPeripheral peripheral: CBPeripheral!, advertisementData: [NSObject : AnyObject]!, RSSI: NSNumber!) {
var localName: String = advertisementData[CBAdvertisementDataLocalNameKey] as String
if (countElements(localName) > 0) {
println("Found Mac: \(localName)")
self.centralManager.stopScan()
} else {
println("Not Found")
}
}
func centralManager(central: CBCentralManager!, didConnectPeripheral peripheral: CBPeripheral!) {
println("1")
}
func centralManager(central: CBCentralManager!, didDisconnectPeripheral peripheral: CBPeripheral!, error: NSError!) {
println("2")
}
func centralManagerDidUpdateState(central: CBCentralManager!) {
switch (central.state) {
case .PoweredOff:
println("Powered Off")
case .PoweredOn:
println("Powered On")
self.centralManager.scanForPeripheralsWithServices(nil, options: nil)
case .Unauthorized:
println("Unauthorized")
case .Unknown:
println("Unknown")
case .Unsupported:
println("Unsupported")
default:
println("Default")
}
}
func applicationDidFinishLaunching(aNotification: NSNotification) {
self.centralManager = CBCentralManager(delegate: nil, queue: nil)
// Insert code here to initialize your application
}
func applicationWillTerminate(aNotification: NSNotification) {
// Insert code here to tear down your application
}
}
我不知道为什么我会收到这个错误,或者我做错了什么
答案 0 :(得分:2)
我能想到两个原因。
您尚未将CoreBluetooth添加为链接框架。
您正在尝试在模拟器中运行代码。然而,模拟器没有可以使用的蓝牙设备。因此,您必须在设备上运行所有蓝牙代码。我已经浏览了一些关于如何使用蓝牙适配器并使其与模拟器一起工作的教程,但我从未成功过。
另外我注意到你没有设置CBCentralManager的委托。
self.centralManager = CBCentralManager(delegate: nil, queue: nil)
不应该是那条线,
self.centralManager = CBCentralManager(delegate: self, queue: nil)
答案 1 :(得分:0)
我对MacOS 10.13有同样的错误,