More fun scripting in Swift: working around an Xcode 9 bug

If you have upgraded to Xcode 9 or 9.1, you may have noticed that it has suddenly started to take exception to perfectly good windows in the Interface Builder. When you run your app, you will see a warning that there is an unknown Window class, and a generic Window is created instead.

xcodebug01

I have successfully ignored this little absurdity for a while now, but finally decided to eliminate it. Some developers have reported that this can cause problems when users try to run their apps, and it is probably best to fix it rather than pretend that it doesn’t exist.

The workaround is to provide a custom NSWindow class. If you don’t spend your whole life immersed in Xcode, that may not appear to be particularly helpful, so here is a workthrough to show you how it can be done. This assumes, of course, that you don’t already use a custom NSWindow class, in which case you shouldn’t be troubled by this bug.

With your project open, add a new file to it using the File… subcommand of the New… command in the File menu. Select a Cocoa Class from the Source files as the template.

xcodebug02

In the next sheet, give the Class a name, typically ending in Window, as a Subclass of NSWindow, using Swift as the Language.

xcodebug03

xcodebug04

Save it into your project folder, and Xcode should then open that new source code file, which will look as barren as this.

xcodebug05

Then switch to the storyboard containing the window which is causing these warnings. Select that window, and select the tool for the Identity Inspector, which has the Custom Class section at its top.

xcodebug06

There, in the Class box, select your new custom Window class.

Now build your app, and the warning should have disappeared. Save everything, and you’re done.