Jared Garcia Jared Garcia - 3 months ago 10
iOS Question

Ambiguous error in Xcode 8 beta 6

After updating to Xcode 8 beta 6, I'm getting the following error:


Value of type 'Any' has no member 'cgRectValue'


on the following block:

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
}
}
}
}


and these two lines specifically:

if let beginKeyBoardFrame = userInfo[UIKeyboardFrameBeginUserInfoKey]!.cgRectValue.origin.y {
if let endKeyBoardFrame = userInfo[UIKeyboardFrameEndUserInfoKey]?.cgRectValue.origin.y {


I may be missing something ever so simple... any suggestions?

Answer

cgRectValue is a property on the NSValue class. What you get out of the userInfo dictionary isn't known to be an NSValue, so you can't access NSValue properties on it.

For the compiler to let you use those properties, you need to let it know that you expect that value to be an NSValue:

if let beginKeyBoardFrame = (userInfo[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue.origin.y {

(This didn't result in compiler errors before because dictionaries in imported Cocoa APIs bridged with a value type of AnyObject. AnyObject has some magic like the ObjC id type, in that the compiler will let you call any method/property on it—with the result wrapped in an Optional because it may not exist. Now that dictionaries have Any as their value type, you need to explicitly say what you expect the value type to be.)

Comments