Xcode 8.0 CBCentralManager问题

编程入门 行业动态 更新时间:2024-10-10 08:21:48
本文介绍了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问题

本文发布于:2023-11-26 05:21:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1632795.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Xcode   CBCentralManager

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!