我目前有18个检查型开关作为NSButtons在SWIFT。我要做的是把他们都送到nsonstate。而不是做
switch1.state = NSOnState
每一次,我都想做一些类似的事情:
let Switches: [NSButton] = [Switch1, Switch2, Switch3, Switch4, Switch5, Switch6, Switch7, Switch8, Switch9, Switch10, Switch11, Switch12, Switch13, Switch14, Switch15, Switch16, Switch17, Switch18]
Switches[0-17].state = NSOnState
问题是我不知道如何正确地执行第二行。它显示错误:“线程1:exc_bad_指令(代码=exc_i386_invop,子代码=0x0)
任何解决方案或建议都会有帮助,谢谢!
(请注意,我不知道objective-c,只知道swift)
最佳答案
forEach
就是你要找的
switches.forEach{ $0.state = NSOnState }
顺便说一下:变量名应该以小写字母开头。