本文介绍了[NSObject:AnyObject]?'在Xcode 6 beta 6中没有名为“subscript”的成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在屏幕上显示的时候,我使用下面的几行代码行来获取键盘的框架。我已经注册到 UIKeyboardDidShowNotification 通知。

I used the below couple of code lines to get the frame of the keyboard when its shown on the screen. I've registered to UIKeyboardDidShowNotification notification.

func keyboardWasShown(notification: NSNotification) {
    var info = notification.userInfo
    var keyboardFrame: CGRect = info.objectForKey(UIKeyboardFrameEndUserInfoKey).CGRectValue()
}

这用于测试版5.我下载了最新的Xcode 6版本,这是beta 6,这个错误发生在第二行。

This used to work in beta 5. I downloaded the latest Xcode 6 version which is beta 6 and this error occurred at the second line.

'[NSObject:AnyObject]?'没有名为objectForKey的成员

谷歌,我遇到了解决方案。我改变了这样,

After some Googling, I came across this solution. And I changed it like so,

var keyboardFrame: CGRect = (info[UIKeyboardFrameEndUserInfoKey] as NSValue).CGRectValue()

但是现在似乎也已经过时了。因为我现在得到这个错误。

But it seems that's also outdated now. Because I get this error now.

'[NSObject:AnyObject]?'没有名为'subscript'的成员

我无法弄清楚这个错误或解决方法。

I can't figure out this error or how to resolve it.

推荐答案

如Xcode 6 beta 6发行说明中所述,已经对可选的一致性进行了大量的基础API审核。
这些更改用 T? T T! c $ c>取决于值是否可以为空(或不分别)。

As mentioned in the Xcode 6 beta 6 release notes, a large number of Foundation APIs have been audited for optional conformance.These changes replace T! with either T? or T depending on whether the value can be null (or not) respectively.

notification.userInfo 现在是一个可选的字典:

class NSNotification : NSObject, NSCopying, NSCoding {
    // ...
    var userInfo: [NSObject : AnyObject]? { get }
    // ...
}

所以你必须解开它如果你知道 userInfo 不是 nil 然后
你可以简单地使用强制展开:

so you have to unwrap it. If you know that userInfo is not nil thenyou can simply use a "forced unwrapping":

var info = notification.userInfo!

但请注意,如果 userInfo nil

否则更好地使用可选的作业:

Otherwise better use an optional assignment:

if let info = notification.userInfo {
    var keyboardFrame: CGRect = (info[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()
} else {
    // no userInfo dictionary present
}

这篇关于[NSObject:AnyObject]?'在Xcode 6 beta 6中没有名为“subscript”的成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 07:17