tokenField:shouldAddObjects:atIndex: not called on loss of focus

Number:rdar://9000060 Date Originated:2/14/2011
Status:Closed Resolved:Duplicate/5686574
Product:Mac OS X Product Version:
Classification:Serious Reproducible:
Summary: NSTokenField does not call its tokenField:shouldAddObjects:atIndex: delegate method upon loss of focus, only when a tokenizing character is pressed, leading to "invalid" tokens being allowed through.

Steps to Reproduce:
- Download attached archive, unarchive, open project within it, build and run target 'Token'. {OpenRadar note: since OpenRadar does not support attachments, this cannot be attached to the OpenRadar version of this bug.}
- In resulting application, type in characters into the token field. Note that as long as you do not leave the token field focus, but trigger tokenization using standard tokenizing characters such as the comma character, no duplicate tokens are allowed (they disappear).
- Now type in a duplicate string (one that has already been tokenized) and press the tab key.

Expected Results:
- tokenField:shouldAddObjects:atIndex: delegate method is called, leading to the invalid duplicate token being made to disappear.

Actual Results:
- tokenField:shouldAddObjects:atIndex: delegate method is *not* called, and invalid duplicate token is tokenized.

Now, if you build the *other* target of this project, Token Workaround, you'll see that there is a way to make this work the way I want in the UI, at least, by implementing tokenField:representedObjectForEditingString: to return an invalid non-string represented object for a duplicate string, and tokenField:displayStringForRepresentedObject: to return nil for that invalid object. This leaves the resulting object value array littered with these invalid objects, however, and causes a Console log message to show up for each duplicate entry. Not ideal!

This behavior has been in existence since at least April 2008, as proven by this blog post:


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!