Oren Edrich Oren Edrich - 1 month ago 8
Swift Question

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 vale of the user default is tue, if it is i want it to follow threw 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)
}


}

Answer

Change

if instruct.value(forKey: "instructions")

to

if instruct.bool(forKey: "instructions")