<!--
{
  "availability" : [
    "macOS: -"
  ],
  "documentType" : "symbol",
  "framework" : "AppKit",
  "identifier" : "/documentation/AppKit/NSAlert",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "AppKit"
    ],
    "preciseIdentifier" : "c:objc(cs)NSAlert"
  },
  "title" : "NSAlert"
}
-->

# NSAlert

A modal dialog or sheet attached to a document window.

```
@MainActor class NSAlert
```

## Overview

The methods of the [`NSAlert`](/documentation/AppKit/NSAlert) class let you specify an alert’s urgency, message text, button titles, and a custom icon. The class also supports a help button and other ways to offer contextual help specific to an alert.

To display an alert as a sheet, call the [`beginSheetModal(for:completionHandler:)`](/documentation/AppKit/NSAlert/beginSheetModal(for:completionHandler:)) method; to display one as an app-modal dialog, use the [`runModal()`](/documentation/AppKit/NSAlert/runModal()) method.

By design, each [`NSAlert`](/documentation/AppKit/NSAlert) object represents a single alert with a specific combination of title, buttons, and other attributes that appear in response to a particular condition. Allocate (`alloc`) and initialize (`init`) an [`NSAlert`](/documentation/AppKit/NSAlert) object for each alert dialog, customize its attributes as described below, and release it once you’re done. If you need to show a particular alert repeatedly, retain and reuse a single instance instead of creating a new one each time.

[`NSAlert`](/documentation/AppKit/NSAlert) objects have the following attributes:

- Type: The importance or urgency of the alert, as shown to the person viewing it. Specified with the [`alertStyle`](/documentation/AppKit/NSAlert/alertStyle) property.
- Message text: The main message of the alert. Specified with [`messageText`](/documentation/AppKit/NSAlert/messageText).
- Informative text: Additional information about the alert. Specified with [`informativeText`](/documentation/AppKit/NSAlert/informativeText).
- Icon: An optional, custom icon to display in the alert, used instead of the default app icon. Specified with [`icon`](/documentation/AppKit/NSAlert/icon).
- Help: A help button that a person can click to get more information about the alert. Use [`helpAnchor`](/documentation/AppKit/NSAlert/helpAnchor) and [`showsHelp`](/documentation/AppKit/NSAlert/showsHelp).
- Response buttons: By default an alert has one response button: the OK button. You can add more response buttons using the [`addButton(withTitle:)`](/documentation/AppKit/NSAlert/addButton(withTitle:)) method.
- Suppression checkbox: A checkbox that lets a person opt out of seeing this particular alert again. Use [`showsSuppressionButton`](/documentation/AppKit/NSAlert/showsSuppressionButton).
- Accessory view: A custom view, such as a text field for entering contact information, that adds extra content to an alert. Use [`accessoryView`](/documentation/AppKit/NSAlert/accessoryView) and [`layout()`](/documentation/AppKit/NSAlert/layout()).

> Note: The `NSAlert` class doesn’t support subclassing.

## Topics

### Creating alerts

[`init(error: any Error)`](/documentation/AppKit/NSAlert/init(error:))

Returns an alert initialized from information in an error object.

### Configuring alerts

[`func layout()`](/documentation/AppKit/NSAlert/layout())

Specifies that the alert must do immediate layout instead of lazily just before display.

[`var alertStyle: NSAlert.Style`](/documentation/AppKit/NSAlert/alertStyle)

Indicates the alert’s severity level.

[`enum Style`](/documentation/AppKit/NSAlert/Style)

The set of alert styles to style alerts in your app.

[`var accessoryView: NSView?`](/documentation/AppKit/NSAlert/accessoryView)

The alert’s accessory view.

[`var showsHelp: Bool`](/documentation/AppKit/NSAlert/showsHelp)

Specifies whether the alert has a help button.

[`var helpAnchor: NSHelpManager.AnchorName?`](/documentation/AppKit/NSAlert/helpAnchor)

The alert’s HTML help anchor.

[`var delegate: (any NSAlertDelegate)?`](/documentation/AppKit/NSAlert/delegate)

The alert’s delegate.

### Displaying alerts

[`func runModal() -> NSApplication.ModalResponse`](/documentation/AppKit/NSAlert/runModal())

Runs the alert as an app-modal dialog and returns the constant that identifies the button clicked.

[`func beginSheetModal(for: NSWindow, completionHandler: ((NSApplication.ModalResponse) -> Void)?)`](/documentation/AppKit/NSAlert/beginSheetModal(for:completionHandler:))

Runs the alert modally as a sheet attached to the specified window.

[`var suppressionButton: NSButton?`](/documentation/AppKit/NSAlert/suppressionButton)

The alert’s suppression checkbox.

[`var showsSuppressionButton: Bool`](/documentation/AppKit/NSAlert/showsSuppressionButton)

Specifies whether the alert includes a suppression checkbox, which you can employ to allow a user to opt out of seeing the alert again.

### Accessing alert text

[`var informativeText: String`](/documentation/AppKit/NSAlert/informativeText)

The alert’s informative text.

[`var messageText: String`](/documentation/AppKit/NSAlert/messageText)

The alert’s message text or title.

### Accessing a custom alert icon

[`var icon: NSImage!`](/documentation/AppKit/NSAlert/icon)

The custom icon displayed in the alert.

### Accessing alert response buttons

[`var buttons: [NSButton]`](/documentation/AppKit/NSAlert/buttons)

The array of response buttons for the alert.

[`func addButton(withTitle: String) -> NSButton`](/documentation/AppKit/NSAlert/addButton(withTitle:))

Adds a button with a given title to the alert.

[`struct ModalResponse`](/documentation/AppKit/NSApplication/ModalResponse)

A set of button return values for modal dialogs.

### Getting alert windows

[`var window: NSWindow`](/documentation/AppKit/NSAlert/window)

The app-modal panel or document-modal sheet that corresponds to the alert.

### Deprecated

[`func beginSheetModal(for: NSWindow, modalDelegate: Any?, didEnd: Selector?, contextInfo: UnsafeMutableRawPointer?)`](/documentation/AppKit/NSAlert/beginSheetModal(for:modalDelegate:didEnd:contextInfo:))

Runs the alert modally as an alert sheet attached to a specified window.

## See Also

  [Dialogs and Special Panels](https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/Dialog/Dialog.html#//apple_ref/doc/uid/10000071i)

## Relationships

### Conforms To

[`Sendable`](/documentation/Swift/Sendable)

[`CustomDebugStringConvertible`](/documentation/Swift/CustomDebugStringConvertible)

[`Equatable`](/documentation/Swift/Equatable)

[`CustomStringConvertible`](/documentation/Swift/CustomStringConvertible)

[`Hashable`](/documentation/Swift/Hashable)

[`NSObjectProtocol`](/documentation/ObjectiveC/NSObjectProtocol)

[`CVarArg`](/documentation/Swift/CVarArg)

### Inherits From

[`NSObject-swift.class`](/documentation/ObjectiveC/NSObject-swift.class)

---

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)