“'NSOffState' 在 Swift 中不可用" [英] "'NSOffState' is unavailable in Swift"

查看:31
本文介绍了“'NSOffState' 在 Swift 中不可用"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 NSView 的扩展,它有一个名为 clearControllersInView() 的简单函数,它获取视图中的所有控制器并将它们设置为默认值(即复选框关闭,弹出窗口和组合到第一个菜单项,文本字段到空字符串).我在 Swift 3 下没有遇到这个问题.

I have an extension of NSView with a simple function called clearControllersInView() which takes all the controllers in the view and sets them to a default value (i.e. checkboxes to off, popups and combos to first menu item, textfields to empty string). I had no problems with this under Swift 3.

我正在使用 Xcode 9 的当前测试版并将此扩展更新到 Swift 4.问题出在处理复选框的部分,当我尝试设置NSOffState 复选框:

I'm using the current beta of Xcode 9 and updating this extension to Swift 4. The problem is in the section handling checkboxes where I'm getting the error "'NSOffState' is unavailable in Swift" when trying to set the checkbox to NSOffState:

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

我在此程序的其他地方检查复选框的值时遇到了相同的错误.我能够通过检查控制器原始值来临时修复这些实例:
if checkbox.state == NSOnState -- 错误
如果 checkbox.state.rawValue == 1 -- 没有错误

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

通常无法通过在此处或 Google 搜索来找到解决方案.任何帮助将不胜感激!

Wasn't able to find a solution by searching here or Google in general. Any help would be greatly appreciated!

推荐答案

First ⌥-click on state 你会看到

First ⌥-click on state you will see

state 的类型已更改为 NSControl.StateValue,这显然是一个枚举或结构体.

The type of state has been changed to NSControl.StateValue, that's clearly an enum or a struct.

所以只需输入 . 并使用代码完成

So just type . and use code completion

PS: 如果 item 是 NSButton 你可以放心地写

PS: if item is NSButton you can safely write

let checkbox = item as! NSButton

或者更好地使用可选绑定

or still better use optional bindings

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

这篇关于“'NSOffState' 在 Swift 中不可用"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆