Search code examples
iosobjective-cios6nsdictionary

Why doesn't some object work with NSDictionary shorthand?


In iOS 6 I can do this:

NSDictionary *d = @{anObject : "key"};

But apparently when the object is a UIImageView or UIWebView (or maybe others too), it crashes with:

'NSInvalidArgumentException', reason: '-[UIWebView copyWithZone:]: unrecognized selector sent

Changing the dictionary declaration to the old way works:

NSDictionary *d = [NSDictionary dictionaryWithObjectsAndKeys:anObject,@"key", nil];

Any idea why this happens?


Solution

  • The syntax is

    NSDictionary *d = @{<key> : <value>, ...};
    

    In your case:

    NSDictionary *d = @{@"key" : anObject};
    

    So your code tried to use anObject as the key. That failed for UIWebView because keys must conform to the NSCopying protocol .