Don’t set NSPopupButton’s menu.items!

Since macOS 10.14 (Mojave) you can set the items of the internal NSMenu of a NSPopupButton in a Cocoa app using simple code like this:

menu.items = […]

But if your app also targets macOS 10.13 (High Sierra), don’t do it! It will fail to load views there using a (partially misleading) error message like this if you do so:

Failed to set (contentViewController) user defined inspected property on (NSWindow): -[NSMenu setItemArray:]: unrecognized selector sent to instance

This may make you think that it’s the main window’s NSMenu which is in error, but you probably created that within a storyboard… so it will be difficult to find the actual issue.

It’s unknown to me why Xcode lets us build the app without any warning if we set the menu.items that way, without an if @available check. And if we only test on Mojave, it won’t break! (It will just break in production!)

The workaround (once you know about the issue) is very simple (while unfortunately verbose):

menu.removeAllItems()
for item in […] { menu.addItem(item) }

About Sorin Dolha

My passion is software development, but I also like physics.
This entry was posted in macOS and tagged , , , , , , , , , . Bookmark the permalink.

Add a reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s