首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >快速读取UserDefault值

快速读取UserDefault值
EN

Stack Overflow用户
提问于 2016-10-21 00:39:06
回答 1查看 257关注 0票数 0

我试图编程一个按钮,以设置一个UserDefault为真。当视图加载时,我希望它检查用户默认值的值是否为true。如果是的话,我想让它执行一行代码。

这是我的代码:

代码语言:javascript
复制
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)
        }
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-10-21 00:44:42

变化

代码语言:javascript
复制
if instruct.value(forKey: "instructions")

代码语言:javascript
复制
if instruct.bool(forKey: "instructions")
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40166630

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档