"NSOffState" not available in Swift "

I have an NSView extension with a simple function called clearControllersInView (), which takes all the controllers in the view and sets them to the default value (i.e. check boxes for pop-ups, pop-ups and combos for the first menu item, text fields for empty lines ) I had no problems with this in Swift 3.

I use the current beta version of Xcode 9 and upgrade this extension to Swift 4. The problem is in the section processing flags, where I get the error "NSOffState" is not available in Swift "when trying to install checkbox for NSOffState:

if item is NSButton {
    let checkbox = item as? NSButton
    checkbox?.state = **NSOffState**  -- *'NSOffState' is unavailable in Swift*
}

I got the same error elsewhere in this program where I check the value of a checkbox. I was able to temporarily fix these instances by checking the raw control value: if checkbox.state == NSOnState - error
  if checkbox.state.rawValue == 1 - no error

Could not find a solution by doing a search here or Google in general. Any help would be greatly appreciated!

+4
source share
1 answer

First ⌥-clickon stateyou will see

enter image description here

The type has statebeen changed to NSControl.StateValue, which is clearly an enumeration or structure.

So just enter .and use code completion

enter image description here


PS: if item is NSButtonyou can safely write

let checkbox = item as! NSButton

if let checkbox =  item as? NSButton { ...
+7

Source: https://habr.com/ru/post/1685549/


All Articles