Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(NSMenuItem): missing setter or instance variable

Tags:

macos

swift

I am encountering a strange error:

2015-04-02 12:20:14.642 test[21167:257788] Failed to connect     
(testApp) outlet from (test.AppDelegate) to (NSMenuItem): missing 
setter or instance variable
inserted id: 122

I occured when a added a menuItem to a menu and connected a function to it.

I do not know what the Problem is. The app works fine but i don't think it is a smart idea to ignore the error. What is meant by setter or instance variable? Why is it needed?

UPDATE: Here is the relevant code:

import Cocoa
import Foundation

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

@IBOutlet weak var window: NSWindow!
@IBOutlet weak var statusMenu: NSMenu!

let statusItem = NSStatusBar.systemStatusBar().statusItemWithLength(-1)

func applicationDidFinishLaunching(aNotification: NSNotification) {
    let icon = NSImage(named: "statusIcon")

    statusItem.image = icon
    statusItem.menu = statusMenu
    // Time for constant repeat
    NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "timerRepeat", userInfo: nil, repeats: true)
}

// Method to call the tracking core
func timerRepeat() {
    //....
}

@IBAction func frontEnd(sender: NSMenuItem) {
    var targetURL : String = NSBundle.mainBundle().resourcePath!
    targetURL = targetURL + "/" + "front.app"
    let workspace = NSWorkspace()
    workspace.launchApplication(targetURL)
}


@IBAction func menuClicked(sender: NSMenuItem) {
    NSApplication.sharedApplication().terminate(self)
}   
}
like image 203
Silve2611 Avatar asked Apr 02 '15 10:04

Silve2611


3 Answers

You have a broken outlet in your xib file. Usually it happens when you set up an outlet to ivar or property which is later deleted or renamed not using Xcode's rename feature.

like image 124
sergeyne Avatar answered Sep 28 '22 19:09

sergeyne


Also make sure that your custom view or view controller class is added to your target. (Project => Target Name => Build Phases => Compile Sources). It's possible that a file is in your project but not your target.

like image 43
Jeremy Conkin Avatar answered Sep 28 '22 17:09

Jeremy Conkin


This happens because you at one point created an @IBOutlet for a storyboard element. You then later removed the code (reference) from your swift file.

I created an example where I create two extra @IBOutlets (I named them 'correctField' and 'incorrectField'- both are incorrect though) and connected them from my storyboard to my swift file. I then removed the code from my swift file. This generates the log as shown in the following figure : enter image description here

To remove this kind of log message, you can do the following:

  1. Go to the 'storyboard' and select the storyboard elements you created connections (@IBOutlets) from.
  2. Open the 'connection inspector' as showed in the figure below
  3. Remove the Referencing Outlets which are incorrect (in my case it is the 'correctField' and 'incorrectField')
  4. Done

This was done in xCode 11

enter image description here

like image 33
Reitenator Avatar answered Sep 28 '22 17:09

Reitenator