0

I am trying to make a main menu for an Mac application as a beginning task to learn about Swift. Here is my code, which does not work.

In the AppDelegate

 import Foundation
 import Cocoa

 @NSApplicationMain

 class AppDelegate: NSObject, NSApplicationDelegate
 {

     @IBOutlet weak var window: NSWindow!

     //private let  mainWindow  =   NSWindow(frame: NSWindow.mainScreen().bounds)
     //let mainWindow =   NSWindow()
     //let statusItem = NSStatusBar.systemStatusBar().statusItemWithLength(NSSquareStatusItemLength)

     func applicationDidFinishLaunching(aNotification: NSNotification)
     {
         // Insert code here to initialize your application
         let menuData = [getMainMenuItems]

         //[makeMainMenu, menuData]

     }

}

And then in a different project file, named Swift_FileManager

 import Foundation
 import Cocoa

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

 func getMainMenuItems (menuData:Array<AnyObject>) -> AnyObject
 {
     //Make a new menu array
     var menuData = [AnyObject]()

     let arrayObject1 = menuArrayObject()
      arrayObject1.title = "Project"
     arrayObject1.subMenuTitles = ["New Project","Open Project","Save Project", "Quit Project"]
     menuData.append(arrayObject1)

     return menuData
 }

The code compiles but the function getMainMenuItems is never called.

Can somebody shed some light on the (probably very simple) issue here? Thanks in advance

3 Answers 3

1
let menuData = [getMainMenuItems]

This line (probably) isn't doing what you think it's doing. What it's doing is creating an array of [(Array<AnyObject>)->(AnyObject)] type (an array of functions that take an array of AnyObjects as input, and return an AnyObject) – and adding your getMainMenuItems function to it.

It's not calling your function at all.

In order to call a function, you need to use a pair of brackets (), containing the required input. In this case, your getMainMenuItems method expects an array of AnyObjects (although it never appears to actually use them).

For example:

let menuData = getMainMenuItems([/* Insert your objects here */])

Although that all being said, I'm not entirely sure that your getMainMenuItems function actually needs an input, as you never use it. I think you're looking for something like this:

func getMainMenuItems() -> [MenuArrayObject] {

    // create an empty array of MenuArrayObjects
    var menuData = [MenuArrayObject]()

    let arrayObject1 = MenuArrayObject()
    arrayObject1.title = "Project"
    arrayObject1.subMenuTitles = ["New Project","Open Project","Save Project", "Quit Project"]
    menuData.append(arrayObject1)

    return menuData
}

Note that the function no longer has an input, and I've replaced AnyObject with [MenuArrayObject]. It's best to be as type specific as you can in Swift (and pretty much every other OOP language), so you should really steer away from AnyObject unless you have good reason to use it.

Now you can just invoke your function like this:

let menuData = getMainMenuItems()

and it will return an array of MenuArrayObjects.

If you're trying to make the function so that it'll add an object to an already existing array, then you can use an inout parameter in your function. This will let you pass an array into your function, which it can modify.

For example:

func getMainMenuItems(inout menuData:[MenuArrayObject]) {

    let arrayObject1 = MenuArrayObject()
    arrayObject1.title = "Project"
    arrayObject1.subMenuTitles = ["New Project","Open Project","Save Project", "Quit Project"]
    menuData.append(arrayObject1)
}

You can then call it like so:

// an existing array of MenuArrayObjects
var menuDataArray = [MenuArrayObject]()

// call function with array – which it will append an element to
getMainMenuItems(&menuDataArray)
Sign up to request clarification or add additional context in comments.

1 Comment

@user1976727 Happy to help :) I have added some extra information about how you should probably be using your getMainMenuItems function to my answer if you're interested.
0

The code let menuData = [getMainMenuItems] is just creating an array containing the function. I'm guessing that you mean to call the function instead. That looks something like:

let menuData = getMainMenuItems([])

Comments

0

In applicationDidFinishLaunching you need to instantiate menuArrayObject:

let myMenuArray = menuArrayObject()

And then call it:

let menuData = myMenuArray.getMainMenuItems()

Your getMainMenuItems method, it seems looking at what you wrote in the method, should be defined as:

func getMainMenuItems() -> [AnyObject]

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.