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

前端之家收集整理的这篇文章主要介绍了objective-c – iOS中不推荐使用的常量前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我构建了一个针对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模拟器上测试了这个.

猜你在找的Xcode相关文章