xcode 'isConnected' 在 iOS 7 中被弃用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24437386/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
'isConnected' deprecated in iOS 7
提问by user1114881
I know this is a stupid question but here goes.
我知道这是一个愚蠢的问题,但这里是。
I have an older app that uses isConnected. Now I get a warning that it is deprecated. Can I just delete this line of code without any ramification or how do I handle this. Sorry for being so dense.
我有一个使用 isConnected 的旧应用程序。现在我收到一个警告,它已被弃用。我可以删除这行代码而不会产生任何后果或我该如何处理。很抱歉这么密集。
here is some code it is from the CBPeripheral framework.
这是来自 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;
}
I think I found the answer should be
我想我发现答案应该是
- (void)cleanup
{
// Don't do anything if we're not connected
if (CBPeripheralStateDisconnected) {
return;
}
I also added @property(readonly) CBPeripheralState state; in my .h
我还添加了@property(readonly) CBPeripheralState 状态;在我的 .h
I don't get an error Can anyone verify this for me?
我没有收到错误 谁能帮我验证一下?
回答by Vincent Guerci
As Apple Documentationsays:
正如Apple 文档所说:
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.)
已连接
一个布尔值,指示外围设备当前是否连接到中央管理器。(只读)(在 iOS 7.0 中已弃用。请改用 state 属性。)
Just replace your code by:
只需将您的代码替换为:
if (self.discoveredPeripheral.state != CBPeripheralStateConnected)
return;
On the other hand, if that's all you have in this method, basically you are doing nothing. So you could just remove that dead code. Which makes me thinks something missing... No cleanup?
另一方面,如果这就是你在这个方法中的全部,基本上你什么都不做。所以你可以删除那些死代码。这让我觉得缺少什么......没有清理?