<!--
{
  "availability" : [
    "macOS: -"
  ],
  "documentType" : "symbol",
  "framework" : "AppKit",
  "identifier" : "/documentation/AppKit/NSMenuDelegate/numberOfItems(in:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "AppKit"
    ],
    "preciseIdentifier" : "c:objc(pl)NSMenuDelegate(im)numberOfItemsInMenu:"
  },
  "title" : "numberOfItems(in:)"
}
-->

# numberOfItems(in:)

Invoked when a menu is about to be displayed at the start of a tracking session so the delegate can specify the number of items in the menu.

```
@MainActor optional func numberOfItems(in menu: NSMenu) -> Int
```

## Parameters

`menu`

The menu object about to be displayed.

## Return Value

The number of menu items in the menu.

## Discussion

If you return a positive value, the menu is resized by either removing or adding items. Newly created items are blank. After the menu is resized, your [`menu(_:update:at:shouldCancel:)`](/documentation/AppKit/NSMenuDelegate/menu(_:update:at:shouldCancel:)) method is called for each item. If you return a negative value, the number of items is left unchanged and [`menu(_:update:at:shouldCancel:)`](/documentation/AppKit/NSMenuDelegate/menu(_:update:at:shouldCancel:)) is not called. If you can populate the menu quickly, you can implement [`menuNeedsUpdate(_:)`](/documentation/AppKit/NSMenuDelegate/menuNeedsUpdate(_:)) instead of [`numberOfItems(in:)`](/documentation/AppKit/NSMenuDelegate/numberOfItems(in:)) and [`menu(_:update:at:shouldCancel:)`](/documentation/AppKit/NSMenuDelegate/menu(_:update:at:shouldCancel:)).

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)