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

objective-c – iOS中不推荐使用的常量

发布时间:2020-12-14 17:36:20 所属栏目:百科 来源:网络整理
导读:我构建了一个针对iOS 3.1.3及更高版本的应用,我遇到了UIKeyboardBoundsUserInfoKey的问题.事实证明它在iOS 3.2及更高版本中已被弃用.我所做的是根据iOS版本使用以下代码来使用正确的密钥: if ([[[UIDevice currentDevice] systemVersion] compare:@"3.2" op
我构建了一个针对iOS 3.1.3及更高版本的应用,我遇到了UIKeyboardBoundsUserInfoKey的问题.事实证明它在iOS 3.2及更高版本中已被弃用.我所做的是根据iOS版本使用以下代码来使用正确的密钥:

if ([[[UIDevice currentDevice] systemVersion] compare:@"3.2" options:NSNumericSearch] != NSOrderedAscending)
    [[aNotification.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] getValue: &keyboardBounds];
else [[aNotification.userInfo valueForKey:UIKeyboardBoundsUserInfoKey] getValue: &keyboardBounds];

这实际上工作正常,但Xcode警告我UIKeyboardBoundsUserInfoKey已被弃用.如何在不压制任何其他警告的情况下摆脱此警告?

另外,有没有办法简单地检查是否定义了UIKeyboardBoundsUserInfoKey以避免检查iOS版本?我试过检查它是否为NULL或nil甚至弱连接UIKit但似乎没有任何工作.

提前致谢

解决方法

由于代码中任何地方都存在不推荐使用的常量会引发警告(并打破我们-Werror用户的构建),您可以使用实际的常量值来查找字典.感谢Apple通常(总是?)使用常量名称作为它的值.

至于运行时检查,我认为你更好testing for the new constant:

&UIKeyboardFrameEndUserInfoKey!=nil

所以,这就是我实际上要做的键盘框架(基于这个other answer):

-(void)didShowKeyboard:(NSNotification *)notification {
    CGRect keyboardFrame = CGRectZero;

    if (&UIKeyboardFrameEndUserInfoKey!=nil) {
        // Constant exists,we're >=3.2
        [[notification.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardFrame];
        if (UIInterfaceOrientationIsPortrait([[UIDevice currentDevice] orientation])) {
            _keyboardHeight = keyboardFrame.size.height;
        }
        else {
            _keyboardHeight = keyboardFrame.size.width;
        }   
    } else {
        // Constant has no value. We're <3.2
        [[notification.userInfo valueForKey:@"UIKeyboardBoundsUserInfoKey"] getValue: &keyboardFrame];
        _keyboardHeight = keyboardFrame.size.height;
    }
}

我实际上在3.0设备和4.0模拟器上测试了这个.

(编辑:李大同)

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

    推荐文章
      热点阅读