更新到Xcode 8 beta 6后,我收到以下错误:
类型的价值'任何'没有会员' cgRectValue'
在以下块中:
var tmpContentViewFrameOrigin: CGPoint?
var tmpCircleViewFrameOrigin: CGPoint?
var keyboardHasBeenShown:Bool = false
func keyboardWillShow(_ notification: Notification) {
keyboardHasBeenShown = true
if let userInfo = (notification as NSNotification).userInfo {
if let beginKeyBoardFrame = userInfo[UIKeyboardFrameBeginUserInfoKey]!.cgRectValue.origin.y {
if let endKeyBoardFrame = userInfo[UIKeyboardFrameEndUserInfoKey]?.cgRectValue.origin.y {
tmpContentViewFrameOrigin = self.contentView.frame.origin
tmpCircleViewFrameOrigin = self.circleBG.frame.origin
let newContentViewFrameY = beginKeyBoardFrame - endKeyBoardFrame - self.contentView.frame.origin.y
let newBallViewFrameY = self.circleBG.frame.origin.y - newContentViewFrameY
self.contentView.frame.origin.y -= newContentViewFrameY
self.circleBG.frame.origin.y = newBallViewFrameY
}
}
}
}
这两行特别是:
if let beginKeyBoardFrame = userInfo[UIKeyboardFrameBeginUserInfoKey]!.cgRectValue.origin.y {
if let endKeyBoardFrame = userInfo[UIKeyboardFrameEndUserInfoKey]?.cgRectValue.origin.y {
我可能会遗漏一些如此简单的东西......有什么建议吗?
答案 0 :(得分:0)
cgRectValue
是NSValue
类的属性。您从userInfo
词典中获得的内容并不是NSValue
,因此您无法访问该NSValue
字词。
为了让编译器允许您使用这些属性,您需要让它知道您希望该值为NSValue
:
if let beginKeyBoardFrame = (userInfo[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue.origin.y {
(之前没有导致编译器错误,因为导入的Cocoa API中的字典与值类型AnyObject
桥接。AnyObject
具有类似ObjC id
类型的魔力,编译器将允许你调用它上面的任何方法/属性 - 结果包含在Optional
中,因为它可能不存在。现在这些字典有Any
作为它们的值类型,你需要明确说出你期望的价值类型。)