2014-10-29 103 views
-1

字典不能識別密鑰類型(更新)

func keyboardWillShow(aNotification: NSNotification) { 
     //Collect information about keyboard using its notification. 
     let info = aNotification.userInfo 
     let duration = (info[UIKeyboardAnimationDurationUserInfoKey] as NSValue) as Double 
     let curve : AnyObject? = info[UIKeyboardAnimationCurveUserInfoKey] 
     let kbFrame : AnyObject? = (info[UIKeyboardFrameEndUserInfoKey] as NSValue).CGRectValue().size 
} 

我怎樣才能得到這些應不

「[NSObject的:AnyObject]讀?沒有一個成員命名'下標'「錯誤?

在xCode的測試版中,它已經工作,但是從xCode 6.1它不再正常工作。

回答

1

userInfo是可選的Dictionary,因此您可以使用可選的綁定來展開值。 CGSize是一個結構體,而不是一個對象,因此將AnyObject改爲CGSize。

if let info = aNotification.userInfo { 
    let duration = (info[UIKeyboardAnimationDurationUserInfoKey] as NSValue) as Double 
    let curve : AnyObject? = info[UIKeyboardAnimationCurveUserInfoKey] 
    let kbFrame: CGSize = (info[UIKeyboardFrameEndUserInfoKey] as NSValue).CGRectValue().size 
}