title: Segmentation fault: 11 with swift
body: <p>I am experimenting with my code to try to add some new features in. After adding in the new UserDefaults I got an error that read "Segmentation fault: 11" and it flagged my secondviewcontoller, the code is down below. Please help</p>
  
  <pre><code>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.
  }
  
  
  }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/T1ldB.png" rel="nofollow"><img src="https://i.stack.imgur.com/T1ldB.png" alt="enter image description here"></a></p>