[NSObject : AnyObject]?' does not have a member named 'subscript' error in Xcode 6 beta 6

前端 未结 1 363
眼角桃花
眼角桃花 2020-11-28 14:00

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 notificat

相关标签:
1条回答
  • 2020-11-28 14:48

    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 is now an optional dictionary:

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

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

    var info = notification.userInfo!
    

    but note that this will crash at runtime if userInfo is 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
    }
    
    0 讨论(0)
提交回复
热议问题