Xcode 8.0 CBCentralManager问题 [英] Xcode 8.0 CBCentralManager Issue

查看:95
本文介绍了Xcode 8.0 CBCentralManager问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近下载了Xcode 8.0,并尝试运行我以前的使用核心蓝牙的项目。

I recently downloaded Xcode 8.0 and trying to run my previous project which uses core bluetooth.

我已在构建设置中启用了使用旧版Swift语言版本,以实现Swift 2.3中的兼容性
一切正常,但是发生了一个问题,

I have enabled Use Legacy Swift Language Version in build setting for compatibility in swift 2.3 everything works, but one Issue occured,

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 会将 CBCentralManagerState 类型返回为int类型,但现在它返回 CBManagerState strong>出现错误,所以我更改为

previously central.state would return CBCentralManagerState type as int but now it returns CBManagerState so got an error so i changed to

 if (central.state == CBManagerState.PoweredOn)

但是 CBManagerState 仅是支持在IOS 10+中运行,但是我想为IOS 8.3+构建它,那么如何更改代码?

But CBManagerState is only supported in IOS 10+ but i want to build it for IOS 8.3+ so how can I change the code?

UPDATE
我也将项目转换为swift 3.0,但仍然存在相同的问题,因此如何在ios版本低于10的手机上运行该项目?

UPDATE I also converted project to swift 3.0, but still same issue, so how can i run this project on mobiles with ios version below 10?

推荐答案

最简单的方法是使用对枚举值的简写引用:

The simplest approach is just to use the short-hand reference to the enumeration value:

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
    }

}

现在,您的代码可以编译而没有错误或警告,并且可以在所有目标上正常工作

Now your code will compile without errors or warnings and works correctly on all targets

这篇关于Xcode 8.0 CBCentralManager问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆