Cocoa app not loading views or running code on High Sierra

可紊 提交于 2019-12-06 06:31:51

问题


I have an app on the Mac AppStore and many users have recently written to say it doesn't work on High Sierra (possibly 10.13.6, its hard to extract specific information from them). I managed to reproduce the issue on a friend's device, however I won't be able to use the device to build with Xcode etc.

The issue seems to be the NSViewController doesn't load it's subviews at all! The grey view controller shown below should have dropdowns and buttons in it.

I also noticed that closing the grey window doesn't close the red transparent window - on Mojave the code to do this runs as expected. A custom shortcut/menu item in the status bar also doesn't appear to run the code it's bound to. Note that the red window is presented via code from the grey window, so some code is managing to run.

This vague thread seems to mention the same issue, as does this question. Neither of them are asking for a programmatic solution however.

Does anyone know how to fix this, or do I need to tell my users to update their OS?

Edit: managed to compile on High Sierra and the views still aren't appearing (nor is there any sign of them in the view debugger). The console says:

2019-02-02 16:53:41.602178+1100 Translate This[20410:36446120] -[NSMenu setItemArray:]: unrecognized selector sent to instance 0x604000069180
2019-02-02 16:53:41.602366+1100 Translate This[20410:36446120] Failed to set (contentViewController) user defined inspected property on (NSWindow): -[NSMenu setItemArray:]: unrecognized selector sent to instance 0x604000069180
2019-02-02 16:54:57.678247+1100 Translate This[20410:36446120] -[NSStoryboard _bundle]: unrecognized selector sent to instance 0x600000000660
2019-02-02 16:54:57.728849+1100 Translate This[20410:36446120] -[NSStoryboard _bundle]: unrecognized selector sent to instance 0x600000000660

0x604000069180 is an NSMenu. I'm not explicitly doing either of the things described in the console output in my code.


回答1:


I was having the same error with a simple menu bar app built on macOS 10.14 but failing when running on earlier OS versions:

[NSMenu setItemArray:]: unrecognized selector sent to instance

When looking at the header file for NSMenu I see:

/* Returns an array containing the receiver's menu items. 
   This property is settable in macOS 10.14 and later. */
open var items: [NSMenuItem]

So you can't set menu.items = someItemsArray but rather use the other apis (menu.addItem(item), menu.removeAllItems(), etc) to fix the issue.

This appears to be an oversight by Apple in Xcode 10 by failing to warn you that it's not settable based on your app's deployment target.

https://openradar.appspot.com/45517851




回答2:


gdub's answer was correct in my case!

Note, however, that for me the issue was not caused by the main NSMenu of the window (which was indeed set by using a storyboard), but we had a popup button (NSPopupButton) which does use a NSMenu inside too, and which we did initialize with items = [...] rather than by calling addItem() in a loop. It was just not obvious cause it was in an inner custom NSView, so it took me hours to dig it down.

I agree that the root issue is Xcode not telling that you need if @available 10.14 for setting items, but for a workaround, please just check your code to see if you set NSMenu.items yourself somewhere, and turn it into a loop with addItem. You did mention you have popup buttons (just like us), so I hope you will be able to fix it and your app to work again on 10.13 too.



来源:https://stackoverflow.com/questions/54452717/cocoa-app-not-loading-views-or-running-code-on-high-sierra

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!