NSAlert fails

I am a newbie here, first request. This is called-from and declared inside my viewcontroller class:

func NrangeAlert() {

let alert = NSAlert()

alert.messageText = "N out of range"

alert.informativeText = "use a number between 1 and 92"

alert.alertStyle = .warning

alert.addButton(withTitle: "OK")

alert.runModal()

print("Select a number between 1 and 92")

}

but fails to show the alert. It compiles.

XCode Version 11.3.1 (11C504)

Swift -version

Apple Swift version 5.1.3 (swiftlang-1100.0.282.1 clang-1100.0.33.15)

Target: x86_64-apple-darwin19.2.0


It successfully prints to the debugger area.

What am I missing?

Answered by Claude31 in 403046022

Is it a MacOS project ?


I tested, in XCode 11.3, MacOS Mojave inside a MacOS project.


It works.

Do you call NrangeAlert() somewhere in your code ?

Are you sure this line of code is executed ?

Please show code.


It successfully prints to the debugger area.

This can occur only once you have answered the alert. So it must show.


Note: name of func should start with lowercase: nRangeAlert()

Accepted Answer

Is it a MacOS project ?


I tested, in XCode 11.3, MacOS Mojave inside a MacOS project.


It works.

Do you call NrangeAlert() somewhere in your code ?

Are you sure this line of code is executed ?

Please show code.


It successfully prints to the debugger area.

This can occur only once you have answered the alert. So it must show.


Note: name of func should start with lowercase: nRangeAlert()

yes, a macOS project.


DOH! You're right, it never executed.


It does present the alert WHEN EXECUTED! lol

BTW it printed to the debugger area by way of other triggers to print it there. All now call the NrangeAlert().


Thanks!

Great.


Don't forget to close the thread.

NSAlert fails
 
 
Q