Expected Declaration Error Using Swift

I am trying to pass the boolean value of UISwitch to another class using NSUserDefaults . For some reason, in a class that contains keys, if , which must be set to NSUserDefaults , cannot read switch declarations.

ViewController.swift

 @IBOutlet var shrimpSwitch: UISwitch! @IBOutlet var nutSwitch: UISwitch! @IBOutlet var dairySwitch: UISwitch! let switchState = NSUserDefaults.standardUserDefaults() if shrimpSwitch.switch.on{ switchState.setBool(true, forKey: "shrimpSwitch") } else{ switchState.setBool(false, forKey: "shrimpSwitch") } if nutSwitch.on{ switchState.setBool(true, forKey: "nutSwitch") } else{ switchState.setBool(false, forKey: "nutSwitch") } if dairySwitch.on{ switchState.setBool(true, forKey: "dairySwitch") } else{ switchState.setBool(false, forKey: "dairySwitch") } 

In the first statement of If (shrimpSwitch.on) he will say "Expected Declaration." I declare that all switches are wrong? Any help would be greatly appreciated. Thanks

+8
xcode swift nsuserdefaults
source share
1 answer

The problem is that you need to put your code inside the method. All you need to do is move it to viewDidLoad () or any other method.

+22
source share

All Articles