I have the code to create and NSAlert in Objective-C but I would now like to create it in Swift.
The alert is to confirm that the user would like to delete a document.
I would like the "delete" button to then run the delete function and the "cancel" one just to dismiss the alert.
How can I write this in Swift?
NSAlert *alert = [[[NSAlert alloc] init] autorelease];
[alert addButtonWithTitle:@"Delete"];
[alert addButtonWithTitle:@"Cancel"];
[alert setMessageText:@"Delete the document?"];
[alert setInformativeText:@"Are you sure you would like to delete the document?"];
[alert setAlertStyle:NSWarningAlertStyle];
[alert beginSheetModalForWindow:[self window] modalDelegate:self didEndSelector:@selector(alertDidEnd:returnCode:contextInfo:) contextInfo:nil];
beginSheetModal(for:completionHandler:)is not deprecated, in fact it may be the more desirable way to handle your modal dialog (in a block). It would also be closer to the old way with thedidEndSelectorand it won't stop the whole application. - Patru