How to call an action when UISwitch changes state?

[yourSwitchObject addTarget:self action:@selector(setState:) forControlEvents:UIControlEventValueChanged]; 

This will call the below method when your switch state changes

- (void)setState:(id)sender 
{
    BOOL state = [sender isOn];
    NSString *rez = state == YES ? @"YES" : @"NO";
    NSLog(rez);
}

Obviously we can do the same with Swift, here is the code (compiled and worked with the latest version of the Swift 3.1)

Add action to your switch button:

mySwitch.addTarget(self, action: #selector(self.switchValueDidChange), for: .valueChanged)

And implement this method:

@objc func switchValueDidChange(sender:UISwitch!) {
    print(sender.isOn)
}

Or even if you are not using the sender you may remove:

func switchValueDidChange() {
    // do your stuff
}