2

I am trying to learn Swift -- current task is making a simple menu for a Mac app from an array of objects that contain strings. The problem now is how to pass the selector, which in the array is a string, but in the code is a function.

The class is

 class menuArrayObject
 {
     var title: String = ""
     var subMenuTitles: [String] = []
     var subMenuSelectors:  [String] = []
 }

Here is my code

         for index2 in 0...counter2 - 1
         {
             let subMenuTitle = arrayObject.subMenuTitles[index2]

             let subMenuSelector = NSSelectorFromString(arrayObject.subMenuSelectors[index2])


             let subMenu = NSMenuItem(title: subMenuTitle, action: #selector(subMenuSelector(_:)),keyEquivalent: "")

             indexMenu.addItem(subMenu)
         }

The error message (on let subMenu =) is: "argument of #selector cannot refer to a property"

Is this do-able? Is this desirable to actually do? Is there a better way?

Thanks in advanceemphasized text

user1976727
  • 71
  • 10

1 Answers1

0

What you are trying to do is totally legit – you can indeed convert a string to a selector, then pass that selector to a menu item.

You're however trying to use the selector literal syntax to initialise a Selector, giving that language construct a Selector as an argument (which is just syntactically wrong), where in fact you can just pass the Selector returned by NSSelectorFromString to the NSMenuItem initialiser call.

The selector literal syntax #selector is used when you have a "fixed" selector in mind that you want to create a Selector for (for instance an instance method of the class you are in). The NSSelectorFromString is intended for this kind of cases like yours where the selector is a variable (now that in Swift 2.2 there is indeed some syntax given for #selector literals!)

import Cocoa

class MenuArrayObject
{
    var title: String = "Foo"
    var subMenuTitles: [String] = ["foo"]
    var subMenuSelectors:  [String] = ["foobar"]
}

let menuArrayObject = MenuArrayObject()

let indexMenu = NSMenu()

for (i, submenuTitle) in menuArrayObject.subMenuTitles.enumerate() {
    let selectorStr = menuArrayObject.subMenuSelectors[i]
    let selector = NSSelectorFromString(selectorStr)
    let item = NSMenuItem(title: submenuTitle, action: selector, keyEquivalent: "")

    indexMenu.addItem(item)
}
mz2
  • 4,672
  • 1
  • 27
  • 47