Skip to content

Instantly share code, notes, and snippets.

@bouchtaoui-dev
Last active April 9, 2019 09:24
Show Gist options
  • Save bouchtaoui-dev/8083f2e7200d07cce0212ac0c24dad7e to your computer and use it in GitHub Desktop.
Save bouchtaoui-dev/8083f2e7200d07cce0212ac0c24dad7e to your computer and use it in GitHub Desktop.
How to improve this snippet of Swift code. I have like more than 10 checkboxes
var socialData: SocialData? = storage.data("social")
...
...
// checkLiveAudio = NSButton (CheckBox button)
...
if let data = socialData {
if data.isAudioEnabled() {
checkLiveAudio.state = NSControl.StateValue.on
} else {
checkLiveAudio.state = NSControl.StateValue.off
}
if data.isAudioPermissionEnabled() {
checkAudioPermission.state = NSControl.StateValue.on
} else {
checkAudioPermission.state = NSControl.StateValue.off
}
}
...
...
// 10 more checkboxes
// This is getting annoying
// I hoped to do something like this:
checkLiveAudio.state = data.isAudioEnabled() ? NSControl.StateValue.on : NSControl.StateValue.off
// Reducing from 5 to 1 line of code, but compiler complains with some errors
// data.isAudioEnabled() returns either false or true, there's no nil or other values, just false or true.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment