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

objective-c – iOS中不推荐使用的常量,第1张

概述我构建了一个针对iOS 3.1.3及更高版本的应用,我遇到了UIKeyboardBoundsUserInfoKey的问题.事实证明它在iOS 3.2及更高版本中已被弃用.我所做的是根据iOS版本使用以下代码来使用正确的密钥: if ([[[UIDevice currentDevice] systemVersion] compare:@"3.2" options:NSNumericSearch] ! 我构建了一个针对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模拟器上测试了这个.

总结

以上是内存溢出为你收集整理的objective-c – iOS中不推荐使用的常量全部内容,希望文章能够帮你解决objective-c – iOS中不推荐使用的常量所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1009835.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存