ios - 无法识别的选择器发送到实例 0x7fe098c09830

标签 ios objective-c swift xcode nsuserdefaults

因此,根据我在 stackoverflow 上看到的内容,我已经看到并尝试对我的代码进行无数次更改,但实际上似乎没有任何效果。当我按下音量按钮时会发生错误,该按钮将写入用户默认数据,因此我可以检查在实际游戏过程中何时播放音频。

我认为这只是我的问题,因为我已经尝试了所有方法。不管怎样,我的代码如下:

//if user presses volume button
@IBAction func DaVol(_ sender: Any)
{
    //if User default for audio is nil, initialize it, otherwise change it where needed
    if audioDefaultIsMuted.value(forKey: "Volume") != nil
    {
        //if the audio has been on turn it off
        let tempAudioDefaultIsMuted = audioDefaultIsMuted.value(forKey: "Volume") as! Bool
        if  tempAudioDefaultIsMuted == false
        {
            //if label is the on music one, set value to true (meaning ismuted will be true)
            //then change label to off label
            audioDefaultIsMuted.set(true, forKey: "Volume")
            audioDefaultIsMuted.synchronize()

            //change to new image
            volumectrl.setImage(#imageLiteral(resourceName: "nowaves"), for: UIControlState.normal)
        }
        else
        {
            //otherwise set value to false
            //then change label to on one
            audioDefaultIsMuted.set(true, forKey: "Volume")
            audioDefaultIsMuted.synchronize()

            //change to new image
            volumectrl.setImage(#imageLiteral(resourceName: "audio"), for: UIControlState.normal)
        }
    }
    let currVolImage = volumectrl.image(for: UIControlState.normal)
    //check curr image and do same shit as before accordingly
    if  currVolImage == #imageLiteral(resourceName: "audio")
    {
        //if label is the on music one, set value to true (meaning ismuted will be true)
        //then change label to off label
        audioDefaultIsMuted.set(true, forKey: "Volume")
        audioDefaultIsMuted.synchronize()

        volumectrl.setImage(#imageLiteral(resourceName: "nowaves"), for: UIControlState.normal)
    }
    else if currVolImage == #imageLiteral(resourceName: "nowaves")
    {
        //otherwise set value to false
        //then change label to on one
        audioDefaultIsMuted.set(false, forKey: "Volume")
        audioDefaultIsMuted.synchronize()

        volumectrl.setImage(#imageLiteral(resourceName: "audio"), for: UIControlState.normal)
    }
}

最佳答案

您从哪里获得 audioDefaultIsMuted,并检查其值。

你可以像这样使用 swift let if 来简化你的代码

if let value = audioDefaultIsMuted.value(forKey: "Volume") {
  print(value)
}

关于ios - 无法识别的选择器发送到实例 0x7fe098c09830,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42311486/

相关文章:

ios - @Published ObservableObject 不更新 SwiftUI View

ios - Xcode 存档以前的版本

ios - 为什么调用 super.init() 会调用我默认的 init()

ios - 如何加载文本文件的内容并将其显示在 SwiftUI TextView 中?

ios - 锁定旋转=iphone上的纵向,ipad没有锁定?

Iphone dev - 带 Storyboard的多 View 应用程序

iphone - 将 2 个 UIImage 添加到一个 UIImage

iPhone : Upload video to Youtube while running app in device

ios - 从 iPhone 应用程序启动 Google map 应用程序。

swift - 无法将类型 'UITableViewCell' (0x10f16f778) 的值转换为 'ProjectName.MyCustomCell' (0x10cc5a568)