import Cocoa
class ViewController: NSViewController {
@IBOutlet weak var helloButton: NSButton!
@IBAction func showAlert(sender: AnyObject) {
var alert = UIAlertController(title: "Hello!", message: "Hello, world!",
preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Close", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
self.helloButton.setTitle("Clicked", forState: UIControlState.Normal)
}
Asked
Active
Viewed 4,122 times
3

WestCoastProjects
- 58,982
- 91
- 316
- 560

yayoTechnology
- 63
- 1
- 4
-
yes, on OS X there is no UIKit :P – Daij-Djan Aug 15 '15 at 11:40
3 Answers
12
UIAlertController
is an iOS class, contained in UIKit
. But as you're doing a Mac app extending NSViewController
and not importing UIKit
(instead Cocoa
), this class is not available to you.
So if you want to create an alert in your Mac app, see for example How to make an alert controller in mac using swift

Community
- 1
- 1

Florian Pfisterer
- 1,205
- 12
- 21
-
The linked solution is wrong. It is suggesting to use UIAlertController, which doesn't exist in Mac. – Houman Jun 10 '21 at 07:50
2
On mac OS you use NSAlert
instead of UIAlertController
. A nice example of using NSAlert
is given in this SO answer.

CodeBrew
- 6,457
- 2
- 43
- 48
0
In my case was removing import Foundation or moving import UIKit above Foundation.
import UIKit
import Foundation //Remove if needed

Marlhex
- 1,870
- 19
- 27