gpt4 book ai didi

ios - 段错误 : 11 with swift

转载 作者:可可西里 更新时间:2023-11-01 00:58:14 26 4
gpt4 key购买 nike

我正在试验我的代码以尝试在其中添加一些新功能。添加新的 UserDefaults 后,我收到一个错误,内容为“Segmentation fault: 11”,它标记了我的 secondviewcontoller,代码在下面。请帮忙

import Foundation
import UIKit
import SpriteKit
import AVFoundation

protocol DestinationViewDelegate {
}
var delegate : GameViewDelegate! = nil

var bombSoundEffect: AVAudioPlayer!
var ghost = SKSpriteNode()

class SecondViewController: UIViewController, GameViewDelegate {

var sw = false
let defaults = UserDefaults.standard

@IBAction func one(_ sender: AnyObject) {
defaults.set(1, forKey: "Sphere")
print("Ghost one was selected")
}

@IBAction func two(_ sender: AnyObject) {
defaults.set(2, forKey: "Sphere")
print("Ghost two was selected")
}

@IBAction func three(_ sender: AnyObject) {
defaults.set(3, forKey: "Sphere")
print("Ghost three was selected")
}

@IBAction func four(_ sender: AnyObject) {
defaults.set(4, forKey: "Sphere")
print("Ghost four was selected")
}

@IBAction func five(_ sender: AnyObject) {
defaults.set(5, forKey: "Sphere")
print("Ghost five was selected")
}

@IBAction func six(_ sender: AnyObject) {
defaults.set(6, forKey: "Sphere")
print("Ghost six was selected")
}


@IBOutlet var mySwitch: UISwitch!

@IBAction func switchpressed(_ sender: AnyObject) {

let defaults = UserDefaults.standard


if mySwitch.isOn{
defaults.set(true, forKey: "SwitchState")

if bombSoundEffect != nil {
bombSoundEffect.stop()
bombSoundEffect = nil

}

}
else{
defaults.set(false, forKey: "SwitchState")

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 :(
}

}


}


override func viewDidLoad() {
super.viewDidLoad()
// Keep this part the same
let defaults = UserDefaults.standard

if (defaults.object(forKey: "SwitchState") != nil) {
mySwitch.isOn = defaults.bool(forKey: "SwitchState")
}

// Check to see if the switch is off, if so, stop the music

}


override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Release any cached data, images, etc that aren't in use.
}


}

enter image description here

最佳答案

问题是这段代码:

@IBAction func one(_ sender: AnyObject) {
defaults.set(1, forKey: "Sphere")
print("Ghost one was selected")
}

您发现了一个编译器错误。尝试像这样解决它:

@IBAction func one(_ sender: AnyObject) {
defaults.set(1 as Any, forKey: "Sphere")
print("Ghost one was selected")
}

您需要为所有 defaults.set 调用执行此操作。我认为这将允许您进行编译。

关于ios - 段错误 : 11 with swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40096859/

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