13

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.

3 Answers 3

11

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.

Sign up to request clarification or add additional context in comments.

3 Comments

@justin: dictionaryWithObjects:forKeys:count: doesn't use NSArrays. It uses C arrays.
whoops - nonsense returned :) it's still possible that NSArrays are created in the process of constructing the dictionary (or calling another constructor), thus the message of the exception.
@justin: An NSDictionary literal does not construct intermediate NSArrays. So unless -setTitleTextAttributes: is doing something funky, that's not the case.
7

The parameters of the literals (values and keys of your dictionary) must not be nil.

If you prefer a constructor which is less strict, you might consider using +[NSDictionary dictionaryWithObjectsAndKeys:] instead.

9 Comments

That's not less strict, it just thinks the arguments have stopped if a nil is found where a key was expected. Notably, if you pass a nil object, you'll get the exact same exception.
Most people tend to think that the nil-object-shortcutting behavior is actually bad because it can silently truncate your dictionary.
I switched to dictionaryWithObjectsAndKeys:, works better than @{} for the time being.
Got exactly same problem and fix with dictionaryWithObjectsAndKeys: Thanks.
This is not a good suggestion, more subtle bugs may arise from using that method, like missing data if for example you would populate a response to a server and send as is. The best action is to check data before inserting into a dict.
|
5

This error seems to be somewhere else. It's pointing to an array error at object '1'. In your case you have a dictionary and object '1' is UITextAttributeTextColor: [UIColor whiteColor] which will never be nil.

2 Comments

yes, for those who come looking to confirm the [<number>] on the exception message, it is the 0-indexed array offset of the failing (nil) object. Tested with: NSString *str = nil; NSDictionary *foo = @{NSFontAttributeName:str,NSForegroundColorAttributeName:@"booboo" };
@robm BOOM! Nice one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.