继承NSApplicationDelegate。

    1. // AppDelegate.swift
    2. import Cocoa
    3. @NSApplicationMain
    4. class AppDelegate: NSObject, NSApplicationDelegate {
    5. //applicationDidFinishLaunching:应用启动前的处理
    6. func applicationDidFinishLaunching(_ aNotification: Notification) {
    7. // Insert code here to initialize your application
    8. }
    9. //applicationWillTerminate:应用推出前的数据资源的释放
    10. func applicationWillTerminate(_ aNotification: Notification) {
    11. // Insert code here to tear down your application
    12. }
    13. /* MARK: - Core Data stack
    14. lazy var persistentContainer: NSPersistentContainer = {
    15. /*
    16. The persistent container for the application. This implementation
    17. creates and returns a container, having loaded the store for the
    18. application to it. This property is optional since there are legitimate
    19. error conditions that could cause the creation of the store to fail.
    20. */
    21. let container = NSPersistentContainer(name: "NewsUpdateMachine")
    22. container.loadPersistentStores(completionHandler: { (storeDescription, error) in
    23. if let error = error {
    24. // Replace this implementation with code to handle the error appropriately.
    25. // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
    26. /*
    27. Typical reasons for an error here include:
    28. * The parent directory does not exist, cannot be created, or disallows writing.
    29. * The persistent store is not accessible, due to permissions or data protection when the device is locked.
    30. * The device is out of space.
    31. * The store could not be migrated to the current model version.
    32. Check the error message to determine what the actual problem was.
    33. */
    34. fatalError("Unresolved error \(error)")
    35. }
    36. })
    37. return container
    38. }()
    39. // MARK: - Core Data Saving and Undo support
    40. @IBAction func saveAction(_ sender: AnyObject?) {
    41. // Performs the save action for the application, which is to send the save: message to the application's managed object context. Any encountered errors are presented to the user.
    42. let context = persistentContainer.viewContext
    43. if !context.commitEditing() {
    44. NSLog("\(NSStringFromClass(type(of: self))) unable to commit editing before saving")
    45. }
    46. if context.hasChanges {
    47. do {
    48. try context.save()
    49. } catch {
    50. // Customize this code block to include application-specific recovery steps.
    51. let nserror = error as NSError
    52. NSApplication.shared.presentError(nserror)
    53. }
    54. }
    55. }
    56. func windowWillReturnUndoManager(window: NSWindow) -> UndoManager? {
    57. // Returns the NSUndoManager for the application. In this case, the manager returned is that of the managed object context for the application.
    58. return persistentContainer.viewContext.undoManager
    59. }
    60. func applicationShouldTerminate(_ sender: NSApplication) -> NSApplication.TerminateReply {
    61. // Save changes in the application's managed object context before the application terminates.
    62. let context = persistentContainer.viewContext
    63. if !context.commitEditing() {
    64. NSLog("\(NSStringFromClass(type(of: self))) unable to commit editing to terminate")
    65. return .terminateCancel
    66. }
    67. if !context.hasChanges {
    68. return .terminateNow
    69. }
    70. do {
    71. try context.save()
    72. } catch {
    73. let nserror = error as NSError
    74. // Customize this code block to include application-specific recovery steps.
    75. let result = sender.presentError(nserror)
    76. if (result) {
    77. return .terminateCancel
    78. }
    79. let question = NSLocalizedString("Could not save changes while quitting. Quit anyway?", comment: "Quit without saves error question message")
    80. let info = NSLocalizedString("Quitting now will lose any changes you have made since the last successful save", comment: "Quit without saves error question info");
    81. let quitButton = NSLocalizedString("Quit anyway", comment: "Quit anyway button title")
    82. let cancelButton = NSLocalizedString("Cancel", comment: "Cancel button title")
    83. let alert = NSAlert()
    84. alert.messageText = question
    85. alert.informativeText = info
    86. alert.addButton(withTitle: quitButton)
    87. alert.addButton(withTitle: cancelButton)
    88. let answer = alert.runModal()
    89. if answer == .alertSecondButtonReturn {
    90. return .terminateCancel
    91. }
    92. }
    93. // If we got here, it is time to quit.
    94. return .terminateNow
    95. *}
    96. }