iOS: "attempt to insert nil object from objects[1]" when creating dictionary

beakr picture beakr · Feb 26, 2013 · Viewed 20.1k times · Source

I'm creating a custom navigation bar class and customizing it's title attributes using the following code:

self.titleTextAttributes = @{ UITextAttributeFont: bariol,
                              UITextAttributeTextColor: [UIColor whiteColor]
                            };

However, when I run the code, it returns the following error message:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSPlaceholderArray initWithObjects:count:]: attempt to insert nil object from objects[1]'

I'm using Xcode 4.6 and running on a device using iOS 6.

Answer

Lily Ballard picture Lily Ballard · Feb 26, 2013

It sounds like your bariol object is nil. You can't store nil in a dictionary.

Edit:

Actually, are you sure that's the right line of code? Your error references NSPlaceholderArray, which suggests it's a problem with a @[] literal, not a @{} literal.