加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

xcode – iOS 7中不推荐使用’isConnected’

发布时间:2020-12-14 18:45:02 所属栏目:百科 来源:网络整理
导读:我知道这是一个愚蠢的问题但是这里有. 我有一个使用isConnected的旧应用程序.现在我收到一个警告,它已被弃用.我可以删除这行代码而不做任何分支,或者我该如何处理.很抱歉这么密集. 这是来自CBPeripheral框架的一些代码. - (void)peripheral:(CBPeripheral *)
我知道这是一个愚蠢的问题但是这里有.

我有一个使用isConnected的旧应用程序.现在我收到一个警告,它已被弃用.我可以删除这行代码而不做任何分支,或者我该如何处理.很抱歉这么密集.

这是来自CBPeripheral框架的一些代码.

- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error
{
    // Deal with errors (if any)
    if (error) {
        NSLog(@"Error discovering characteristics: %@",[error localizedDescription]);
        [self cleanup];
        return;
    }
}
- (void)cleanup
{
    // Don't do anything if we're not connected
    if (!self.discoveredPeripheral.isConnected) // here is where the warning comes {
        return;
    }

我想我找到的答案应该是

- (void)cleanup
    {
        // Don't do anything if we're not connected
        if (CBPeripheralStateDisconnected)  {
            return;
        }

我还添加了@property(readonly)CBPeripheralState状态;在我的.h

我没有收到错误任何人都可以为我验证这个吗?

解决方法

正如 Apple Documentation所说:

isConnected

A Boolean value indicating whether the peripheral is currently connected to the central manager. (read-only) (Deprecated in iOS 7.0. Use the state property instead.)

只需将代码替换为:

if (self.discoveredPeripheral.state != CBPeripheralStateConnected)
    return;

另一方面,如果这就是你在这种方法中所拥有的,那么基本上你什么都不做.所以你可以删除那些死代码.这让我觉得缺少了什么…没有清理?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读