Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dictionary doesn't recognize key type

With the beta 3 of Xcode the following piece of code doesn't work anymore:

func keyboardWasShown (notification: NSNotification)
{        
        var info = notification.userInfo
        keyboardSize = info.objectForKey(UIKeyboardFrameBeginUserInfoKey).CGRectValue().size        
}

at the instruction:

keyboardSize = info.objectForKey(UIKeyboardFrameBeginUserInfoKey).CGRectValue().size

XCode return the error [NSObject : AnyObject] does not have a member named objectForKey.

So i changed the code like this:

func keyboardWasShown (notification: NSNotification)
{

        var info = notification.userInfo
        keyboardSize = info[UIKeyboardFrameBeginUserInfoKey].CGRectValue().size

}

but XCode returns the error "String is not a subtype f DictionaryIndex"

like image 589
Andorath Avatar asked Jul 08 '14 09:07

Andorath


People also ask

Can a dictionary key be any type?

Second, a dictionary key must be of a type that is immutable. For example, you can use an integer, float, string, or Boolean as a dictionary key. However, neither a list nor another dictionary can serve as a dictionary key, because lists and dictionaries are mutable.

How do you fix KeyError?

How to Fix KeyError in Python. To avoid the KeyError in Python, keys in a dictionary should be checked before using them to retrieve items. This will help ensure that the key exists in the dictionary and is only used if it does, thereby avoiding the KeyError . This can be done using the in keyword.

How do I know if a key isnt in the dictionary?

Check If Key Exists using the Inbuilt method keys() Using the Inbuilt method keys() method returns a list of all the available keys in the dictionary. With the Inbuilt method keys(), use if statement with 'in' operator to check if the key is present in the dictionary or not.


1 Answers

As of Xcode 6 Beta 3, NSDictionary* is now imported from Objective-C APIs as [NSObject : AnyObject] (this change is documented in the release notes).

So you can access the dictionary value with info[UIKeyboardFrameBeginUserInfoKey]. This gives an AnyObject which you have to cast to NSValue, so that CGRectValue() can be applied:

var info = notification.userInfo
let keyboardSize = (info[UIKeyboardFrameBeginUserInfoKey] as NSValue).CGRectValue().size

The error message

'String' is not a subtype of 'DictionaryIndex'

is quite misleading, the problem with your second attempt is only the missing cast.

Update for Xcode 6.3: notification.userInfo is an optional dictionary now and must be unwrapped, e.g. with optional binding. Also the forced cast operator as has been renamed to as!:

if let info = notification.userInfo {
    let keyboardSize = (info[UIKeyboardFrameBeginUserInfoKey] as! NSValue).CGRectValue().size
} else {
    // no user info
}

We can forcefully unwrap the dictionary value because it is documented to be a NSValue of a CGRect.

like image 60
Martin R Avatar answered Oct 06 '22 23:10

Martin R