Home > Back-end >  how to save user settings in userDefaults - Swift
how to save user settings in userDefaults - Swift

Time:11-14

I'm working on a CountDown Timer app, i set the timer to set an alarm if the time is up. I made a settings VC to save whether the user wants the sound and vibration enabled or not. i am trying to save the user's preferences to userDefaults using this code

import UIKit

class SettingsViewController: UIViewController {
    
    @IBOutlet weak var soundLabel: UILabel!
    @IBOutlet weak var vibrationLabel: UILabel!
    @IBOutlet weak var soundSwitch: UISwitch!
    @IBOutlet weak var vibrationSwitch: UISwitch!
    
    var soundEnabled = true
    var vibrationEnabled = true
    
    let userDefaults = UserDefaults.standard
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        soundLabel.text = "Sound Enabled"
        soundLabel.textColor = .white

        vibrationLabel.text = "Vibration Enabled"
        vibrationLabel.textColor = .white
        
        soundSavedSettings()
        vibrationSavedSettings()
        
    }
    
    

    @IBAction func soundSwitchPressed(_ sender: UISwitch) {
        if soundEnabled {
          
            soundEnabled = false
            soundLabel.text = "Sound Disabled"


        } else {
            soundEnabled = true
            soundLabel.text = "Sound Enabled"
        }
        userDefaults.set(soundEnabled, forKey: "soundEnabled")
    }
    
    @IBAction func vibrationSwtichPressed(_ sender: UISwitch) {
        
        if vibrationEnabled {
            vibrationEnabled = false
            vibrationLabel.text = "Vibration Disabled"

            print("\(vibrationEnabled.description)")
        
        } else {
            vibrationEnabled = true
            vibrationLabel.text = "Vibration Enabled"
    
            print("\(vibrationEnabled.description)")
       
        }
        userDefaults.set(vibrationEnabled, forKey: "vibrationEnabled")
        
    }
    
    func soundSavedSettings() {
    
        soundEnabled = userDefaults.bool(forKey: "soundEnabled")
        soundSwitch.isOn = soundEnabled
        if soundSwitch.isOn {
        soundLabel.text = "Sound Enabled"
        } else {
        soundLabel.text = "Sound Disabled"
        }
    }
    
    func vibrationSavedSettings() {
   
        vibrationEnabled = userDefaults.bool(forKey: "vibrationEnabled")
        vibrationSwitch.isOn = vibrationEnabled
        if vibrationSwitch.isOn {
        vibrationLabel.text = "Vibration Enabled"
        } else {
        vibrationLabel.text = "Vibration Disabled"
        }
    }
}

but when i run the app and set the sound or vibration settings to false using the switch, when the countdown timer reaches 0, the app crashes and it gives me this error:

Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value

for soundSwitch.isOn

.. not sure how to go about this as i've tried different approaches and nothing worked for me .. would appreciate some help ..

CodePudding user response:

I got the issue solved.

it turned out i was passing the original value of soundEnabled and soundDisabled to the Timer view controller without considering the updated value in the userDefault which caused a crash because the soundSwitch.isOn and vibrationSwitch.isOn only exist in the settings view controller and thus have no value (I think that's the explanation) ..

i solved it by passing

    userDefaults.bool(forKey: "soundEnabled")

and

    userDefaults.bool(forKey: "vibrationEnabled")

directly to the Timer view controller. Now it's updating properly.

CodePudding user response:

Please change your code with following:

soundSwitch.isOn = soundEnabled ?? false or

soundSwitch.isOn = soundEnabled ?? true
  • Related