1

After spent all the afternoon and the night to try understand how it works, I try to ask here to resolve my problem.

I've created a Cocoa App in Xcode. I deleted the default MainMenu.xib file in order to create my own file.

Now I have:

  • a file named AppDelegate.swift with the following part of code

    @NSApplicationMain class AppDelegate: NSObject, NSApplicationDelegate {

  • a file named Window.swift with the following code:

    class Window: NSWindowController {

  • A file named Window.xib that has no connection (outlet set) since I try to understand how set it so I have deleted all the already created in order to try to recreate it.

The problem is that, even I can link a button (previously inserted with the Interface Builder into the window) in the Window : NSWindowsController class, it return the error

Failed to connect (button) outlet from (NSApplication)
to (NSButton): missing setter or instance variable 

I don't understand why since the only connection is the connection now defined and no more possible connection are possible to do (I've tried to link some component around the screen in order to see some anchor zone but no appears).

So, even if there are some questions that analyse this problem given the answer that the user have done something wrong, I try to ask to explain how the connection are performed in order to recreate it and understand where is the problem.

Only for completeness, in the Windows.xib, nether File's Owner, First Responder or Application shows links in the Connections inspector panel.

2
  • Did you delete MainMenu.nib from the app (menu Project->Clean)? Which object is connected to the button? In which class is the button outlet defined? If the button is connected to the File's owner, how do you load the nib? Commented Jan 28, 2018 at 9:31
  • 1
    No object is connected to the button. / The button outlet is not defined in any class, is just a drag-and-drop element. / The file's owner has no connections. Commented Jan 28, 2018 at 11:49

2 Answers 2

4
  1. remove @NSApplicationMain annotation in AppDelegate.swift
  //@NSApplicationMain 
  class AppDelegate: NSObject, NSApplicationDelegate {
  1. add main.swift in the project. e.g.
import Cocoa

autoreleasepool {
 let delegate = AppDelegate()
 // NSApplication delegate is a weak reference,
 // so we have to make sure it's not deallocated.
 withExtendedLifetime(delegate, {
     let application = NSApplication.shared
     application.delegate = delegate
     application.run()
     application.delegate = nil
 })
}

Reference: How is MainMenu.xib found in Cocoa?

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

Comments

1

First, if you're new to Mac and Xcode development, stick to this rule: don't delete things until you known what they are.

Mac app projects are complex things with lots—and I means lots—of interconnected parts.

One of those parts is the Info.plist file that's imbedded in the application's bundle. It contains important information about your app (what document types it creates/opens, the version of the app, and so on). But one of it's most important functions is in launching your application.

There are a few, critical, pieces of information in that file. Several of them determine how your application gets started. Specifically, NSMainNibFile ("Main nib file base name") determines the nib file that will get loaded to create your application objects.

In the app project you created, this would have defaulted to—drum roll, please—MainMenu.

The nib file named in that property traditionally contains the working menu for the application (thus, its name). But it's also responsible for instantiating your NSApplicationDelegate and connecting that delegate object to your NSApplication object. If those things doesn't happen at launch you, literally, don't have an application.

My advice is to start over with a new project. Go ahead and add to it. Define actions in your delegate or window controller, create some outlets, create some view or control objects, then connect them up, set some breakpoints, and have fun.

But don't go deleting things until you understand what role they play and how the app gets assembled because ... it's complicated.

Comments

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.