Oren Edrich
Oren Edrich

Reputation: 674

Reading UserDefault Values with swift

I am trying to program a button to set a UserDefault to true. And when the view loads I want it to check if the value of the user default is true. If it is I want it to follow through with a line of code.

Here is my code:

import UIKit
import SpriteKit
import AVFoundation

var bombSoundEffect: AVAudioPlayer!
let instruct = UserDefaults.standard

class GameViewController: UIViewController {
    @IBOutlet weak var intructions: UIButton!
    @IBAction func intructions(_ sender: AnyObject) {
        instruct.set(true, forKey: "instructions")
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        if instruct.value(forKey: "instructions") {
            intructions.isHidden = true
        }

        let path = Bundle.main.path(forResource: "Untitled2.wav", ofType:nil)!
        let url = URL(fileURLWithPath: path)

        do {
            let sound = try AVAudioPlayer(contentsOf: url)
            bombSoundEffect = sound
            sound.numberOfLoops = -1
            sound.play()
        } catch {
            // couldn't load file :(
        }

        if let scene = GameScene(fileNamed:"GameScene") {
            // Configure the view.
            let skView = self.view as! SKView
            skView.showsFPS = false
            skView.showsNodeCount = false

            /* Sprite Kit applies additional optimizations to improve rendering performance */
            skView.ignoresSiblingOrder = true

            /* Set the scale mode to scale to fit the window */
            scene.scaleMode = .aspectFill
            scene.size = self.view.bounds.size

            skView.presentScene(scene)
        }
    }

Upvotes: 0

Views: 268

Answers (1)

matt
matt

Reputation: 535140

Change

if instruct.value(forKey: "instructions")

to

if instruct.bool(forKey: "instructions")

Upvotes: 1

Related Questions