Issue with @IBOutlet textField: NSTextField!


This is the topic posted before. But I had a different situation. After I insert the IBOutlet line in “MainWindowController.swift”, I didn’t get a textField outlet in File’s Owner and an ! sign shows up in the window outlet. It says “MainWindowController does not have an outlet named window.”

According to the previous post, I checked the class name and found no error. I don’t know how to tackle this. Any help would be appreciated!


It’s fairly difficult to debug invisible code. Post the entirety of your MainWindowController.swift file. Then highlight your code and click on the Code button at the top of the text box where you pasted your code.

Here’s the code

import Cocoa

class MainWindowController: NSWindowController {

    @IBOutlet weak var textField: NSTextField!
    override func windowDidLoad() {

        // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.

Okay, your MainWindowController.swift file looks fine. Is there a file called MainWindowController.xib in your project? There should be one. On p. 7, the book instructs you to check a checkbox when you create MainWindowController.swift, so that a file named MainWindowController.xib is also created. The checkbox you are supposed to check is shown in Figure 1.9. In MainMenu.xib, the File’s Owner will not have the outlet for the textField–the File’s Owner in MainWindowController.xib will have the outlet. That’s because when you create the two files MainWindowController.swift and MainWindowController.xib at the same time, Xcode automatically creates connections between them, and thereafter @IBOutlets declared in MainWindowController.swift will show up in MainWindowController.xib.

Thanks for the suggestion! I did check the checkbox when I new the MainWindowController.swift file.

I just reopened the project and found everything is correct now. It’s weird. Thanks anyway!

What version of Xcode are you using? The only thing I can think of is that you were accidentally looking in MainMenu.xib and not MainWindowController.xib. Is that possible? Did you perhaps go into MainWindow.xib to delete the window, and then look for the @IBOutlet while there?

I don’t think so. I guess the reason is that I use dropbox to sync the project file. I now switch it to iCloud Drive.

I ran into this issue, and indeed it was Dropbox that was causing it. Moving to a non-dropbox directory fixed the issue.

The issue seems to be caused when copy/pasting the code rather than typing it in manually.

For example, when copying the code from … 01s04.html there appears to be some additional formatting that is copied over into XCode - which it rejects as errors.