Search code examples
swiftxcodecheckboxnsbutton

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}


Solution

  • 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)