gpt4 book ai didi

ios - swift 错误 : Could not cast value of type 'NSTaggedPointerString' (0x1a1264378) to 'NSNumber' (0x1a126f900)

转载 作者:搜寻专家 更新时间:2023-10-31 22:59:51 25 4
gpt4 key购买 nike

我在 Xcode (swift) 中遇到这个错误:

Could not cast value of type 'NSTaggedPointerString' (0x1a1264378) to 'NSNumber' (0x1a126f900).

我用//ERROR: ("...") 明确了错误在哪里

代码:

import UIKit
import iAd
import AVFoundation
import AudioToolbox

let MAX : UInt32 = 4
let MIN : UInt32 = 1

class ViewController: UIViewController, ADBannerViewDelegate {


var chances = 10
var won = 0
var highscore = 0
var random = Int(arc4random_uniform(MAX) + MIN)
var sound = 1022 //sound if you won
var sound2 = 1105 //sound if reset
var sound3 = 1321 //sound if lose
var welkmute = 1 //mute = 1 and unmute = 2

@IBOutlet weak var timeswon: UILabel!
@IBOutlet weak var Banner: ADBannerView!
@IBOutlet weak var Highscores: UILabel!
@IBOutlet weak var mute: UIButton!
@IBOutlet weak var unmute: UIButton!
@IBOutlet weak var chancesleft: UILabel!




func unmUte() {
unmute.setTitleColor(UIColor.redColor(), forState: UIControlState())
mute.setTitleColor(UIColor.blueColor(), forState: UIControlState())

}

func mUte() {
mute.setTitleColor(UIColor.redColor(), forState: UIControlState())
unmute.setTitleColor(UIColor.blueColor(), forState: UIControlState())
}


override func prefersStatusBarHidden() -> Bool {
return true
}

@IBAction func Mute(sender: AnyObject) {
sound = 4095
sound2 = 4095
sound3 = 4095
mUte()

defaults.setObject("1", forKey: "GetMute")
defaults.synchronize()

}

@IBAction func Unmute(sender: AnyObject) {
sound = 1022
sound2 = 1105
sound3 = 1321
unmUte()

defaults.setObject("2", forKey: "GetMute")
defaults.synchronize()

}

override func viewDidLoad() {
super.viewDidLoad()

//unmUte()

Banner.hidden = true
Banner.delegate = self
self.canDisplayBannerAds = true


if defaults.valueForKey("GetHighScore") != nil {
highscore = defaults.valueForKey("GetHighScore") as! NSInteger
}

Highscores.text = "Highscore: \(highscore)"



if defaults.valueForKey("GetMute") != nil {
welkmute = defaults.valueForKey("GetMute") as! NSInteger //ERROR: (Thread 1: breakpoint 3.1)

if welkmute == 1 {
mUte()
}

if welkmute == 2 {
unmUte()

}
}
}

最佳答案

您正在将 String 保存到 NSUserDefaults,然后您正在读取 NSNumber。这是不可能的,错误消息就是这么说的。

NSUserDefaults 有专门的方法来处理整数

设置:

  • Swift 1 和 2:

    defaults.setInteger(2, forKey: "GetMute")
  • swift 3:

    defaults.set(2, forKey: "GetMute")

    defaults.synchronize() 通常不需要。

获取:

  • Swift 1 和 2:

    welkmute = defaults.integerForKey("GetMute") 
  • swift 3:

    welkmute = defaults.integer(forKey:"GetMute") 

两个注意事项:

  • 切勿在对象的 NSUserDefaults 中使用 valueForKey。它是 objectForKey
  • 尽可能使用 Swift 原生类型(Int 而不是 NSInteger)。

关于ios - swift 错误 : Could not cast value of type 'NSTaggedPointerString' (0x1a1264378) to 'NSNumber' (0x1a126f900),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38231386/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com