Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking state of NSButton Checkbox on Xcode for macOS apps swift 3

I have an NSButton Checkbox on Xcode using Swift 3 for making macOS applications. I'm trying to check the state (whether its on or off). Some options I have found, such as : if ([Switch1 state] == NSOnState) { //CODE} When I do this, it tells me to inout a comma after "Switch 1". Is this right, or is this version of code too old for Swift 3?

`@IBOutlet weak var Switch1: NSButton!
 override func viewDidLoad() {
    super.viewDidLoad()
    if ([Switch1 state] == NSOnState) {
        print("On")
    }  
  }`

UPDATE: For those looking at this later as a reference, the correct line was if (Switch1.state == NSOnState) {//CODE}

like image 949
Hussein Esmail Avatar asked Aug 14 '17 16:08

Hussein Esmail


1 Answers

Xcode 9 Swift 4 I found this post which solved the problem for me. "'NSOffState' is unavailable in Swift"

(In short, the NSOnState is replaced with .on)

like image 174
Steve Thoonen Avatar answered Nov 05 '22 10:11

Steve Thoonen