如何使用 swift 在 mac 中制作警报控制器

How to make an alert controller in mac using swift

我是 Swift 的新手,我正在尝试为 Mac OS X 制作乐谱收集程序。我确实了解 Swift 上的编程iOS,但我对 OS X Swift 了解不多。所以我为 OS X 编写了一个程序。我几乎完成了它,但我想知道如何使用 swift 在 mac 中制作警报控制器。我知道如何在 iso 上执行此操作,但我不知道如何在 OS X 上执行此操作,而且我想知道如何在中使用 .isEmpty OS X.

import Cocoa

class ViewController: NSViewController {

@IBOutlet var Maths: NSTextField! = NSTextField()
@IBOutlet var Science: NSTextField! = NSTextField()
@IBOutlet var English: NSTextField! = NSTextField()
@IBOutlet var Religious: NSTextField! = NSTextField()
@IBOutlet var History: NSTextField! = NSTextField()
@IBOutlet var Geography: NSTextField! = NSTextField()
@IBOutlet var ICT: NSTextField! = NSTextField()
@IBOutlet var Computing: NSTextField! = NSTextField()

@IBOutlet var Total: NSTextField!
@IBOutlet var AVG: NSTextField!

@IBOutlet var StudentName: NSTextField!
@IBOutlet var StudentClass: NSTextField!

@IBOutlet var Report: NSTextField!






override func viewDidLoad() {
    super.viewDidLoad()



}

override var representedObject: AnyObject? {
    didSet {
    // Update the view, if already loaded.


    }
}

@IBAction func CountTotalnAVG(sender: NSButton) {

    var Mathsc = Maths.stringValue.toInt()
    var Sciencec = Science.stringValue.toInt()
    var Englishc = English.stringValue.toInt()
    var Religiousc = Religious.stringValue.toInt()
    var Historyc = History.stringValue.toInt()
    var Geographyc = Geography.stringValue.toInt()
    var ICTc = ICT.stringValue.toInt()
    var computingc = Computing.stringValue.toInt()

    /*
    var add = (Mathsc?.advancedBy(Sciencec!).advancedBy(Englishc!).advancedBy(Religiousc!).advancedBy(Historyc!).advancedBy(Geographyc!).advancedBy(ICTc!).advancedBy(computingc!))
    */

    var add: Int = (Mathsc! + Sciencec! + Englishc! + Religiousc! + Historyc! + Geographyc! + ICTc! + computingc!)

    if (Mathsc! == " ") {

    }
    else{
        Total.stringValue = "\(add)"

        var divide:Int = add/8

        AVG.stringValue = "\(divide)"
    }
}

@IBAction func GetReport(sender: NSButton) {

    var Mathsc = Maths.stringValue.toInt()
    var Sciencec = Science.stringValue.toInt()
    var Englishc = English.stringValue.toInt()
    var Religiousc = Religious.stringValue.toInt()
    var Historyc = History.stringValue.toInt()
    var Geographyc = Geography.stringValue.toInt()
    var ICTc = ICT.stringValue.toInt()
    var computingc = Computing.stringValue.toInt()

    func getGrade(score: Int) -> String {
        let result: String
        switch score {
        case 80...100 :
            result = "A"
        case 75..<80 :
            result = "B"
        case 60..<75 :
            result = "C"
        case 50..<60 :
            result = "D"
        case 40..<50 :
            result = "E"
        case 0..<40 :
            result = "F"
        default:
            result = "F"
        }
        return result
    }

    Report.stringValue = "Grades you got: \(getGrade(Mathsc!) ,(getGrade(Sciencec!)))"



}


}

UIAlertController相关的iOS和macOS没有区别...

    let title = "Do you want to save the changes made to the document?".localized
    let description = "Your changes will be lost if you don’t save them.".localized
    
    let discard = "Don't Save".localized
    let save = "Save".localized
    let cancel = "Cancel".localized
    
    let alert = UIAlertController(title: title, message: description, preferredStyle: .alert)
    let discardAction = UIAlertAction(title: discard, style: .destructive) { _ in
        // don't save action here
    }
    let saveAction = UIAlertAction(title: save, style: .default) { _ in
        // save action here
    }
    let cancelAction = UIAlertAction(title: cancel, style: .cancel) { _ in }
    
    alert.addAction(discardAction)
    alert.addAction(cancelAction)
    alert.addAction(saveAction)

    yourViewController.present(alert, animated: true, completion: nil)