Xcode 8.0 CBCentralManager 问题
Xcode 8.0 CBCentralManager Issue
我最近下载了 Xcode 8.0 并尝试 运行 我以前使用核心蓝牙的项目。
我在 swift 2.3 的构建设置中启用了 使用旧版 Swift 语言版本
一切正常,但出现了一个问题,
func centralManagerDidUpdateState(central: CBCentralManager)
{
print("state is \(central.state.rawValue)")
if (central.state == CBCentralManagerState.PoweredOn)
{
self.centralManager?.scanForPeripheralsWithServices([serviceUUID], options: nil)
}
else
{
// do something like alert the user that ble is not on
}
}
以前 central.state 会 return CBCentralManagerState 类型为 int 但现在 returns CBManagerState 出现错误所以我改为
if (central.state == CBManagerState.PoweredOn)
但是 CBManagerState 仅在 IOS 10+ 中受支持,但我想为 IOS 8.3+ 构建它,那么如何更改代码?
更新
我也将项目转换为 swift 3.0,但仍然是同样的问题,所以我如何在 ios 版本低于 10 的手机上 运行 这个项目?
最简单的方法就是使用枚举值的简写引用:
func centralManagerDidUpdateState(central: CBCentralManager)
{
print("state is \(central.state.rawValue)")
if (central.state == .PoweredOn)
{
self.centralManager?.scanForPeripheralsWithServices([serviceUUID], options: nil)
}
else
{
// do something like alert the user that ble is not on
}
}
现在您的代码将在没有错误或警告的情况下编译,并且可以在所有目标上正常工作
我最近下载了 Xcode 8.0 并尝试 运行 我以前使用核心蓝牙的项目。
我在 swift 2.3 的构建设置中启用了 使用旧版 Swift 语言版本 一切正常,但出现了一个问题,
func centralManagerDidUpdateState(central: CBCentralManager)
{
print("state is \(central.state.rawValue)")
if (central.state == CBCentralManagerState.PoweredOn)
{
self.centralManager?.scanForPeripheralsWithServices([serviceUUID], options: nil)
}
else
{
// do something like alert the user that ble is not on
}
}
以前 central.state 会 return CBCentralManagerState 类型为 int 但现在 returns CBManagerState 出现错误所以我改为
if (central.state == CBManagerState.PoweredOn)
但是 CBManagerState 仅在 IOS 10+ 中受支持,但我想为 IOS 8.3+ 构建它,那么如何更改代码?
更新 我也将项目转换为 swift 3.0,但仍然是同样的问题,所以我如何在 ios 版本低于 10 的手机上 运行 这个项目?
最简单的方法就是使用枚举值的简写引用:
func centralManagerDidUpdateState(central: CBCentralManager)
{
print("state is \(central.state.rawValue)")
if (central.state == .PoweredOn)
{
self.centralManager?.scanForPeripheralsWithServices([serviceUUID], options: nil)
}
else
{
// do something like alert the user that ble is not on
}
}
现在您的代码将在没有错误或警告的情况下编译,并且可以在所有目标上正常工作