2016-07-29 6 views
-3

Ich bin ziemlich neu und versuchen, das richtige Format herauszufinden, um den Fehler im Titel zu lösen. Ich gehe in die Leitung: lass audioPath = NSBundle.mainBundle(). PathForResource ("Pugs.m4a", ofType: nil)!Fehler: Schwerwiegender Fehler: unerwartet gefunden Null beim Entpacken ein optionaler Wert

Ich weiß, ich muss etwas vermissen, nur nicht sicher, wo.

Import UIKit Import AVFoundation

Klasse Viewcontroller: UIViewController {

@IBOutlet var playButton: UIButton! 

var playPug = 1 

var player: AVAudioPlayer! 

@IBAction func playPressed(sender: AnyObject) { 


    let audioPath = NSBundle.mainBundle().pathForResource("Pugs.m4a", ofType: nil)! 

    let url = NSURL(fileURLWithPath: audioPath) 

    do { 

     if playPug == 1 { 
      let sound = try AVAudioPlayer(contentsOfURL: url) 
      player = sound 
      sound.play() 
      playPug = 2 
      playButton.setImage(UIImage(named:"pause_Icon.png"),forState:UIControlState.Normal) 
     } else { 
      player.pause() 
      playPug = 1 
      playButton.setImage(UIImage(named:"play_Icon.png"),forState:UIControlState.Normal) 
     } 

    } catch { 
     print(error) 
    } 

} 
+2

Wenn Sie die Dokumentation von Apple betrachten, werden Sie sehen, dass die Methode 'init (contentsOfURL url: NSURL)' ist und Swift 2-Fehlerbehandlung verwendet. Die Dokumentation sollte immer Ihre erste Adresse sein. Link zur 'AVAudioPlayer'-Dokumentation: https://developer.apple.com/library/ios/documentation/AVFoundation/Reference/AVAudioPlayerClassReference/#/apple_ref/occ/instm/AVAudioPlayer/initWithContentsOfURL:error:. Link zur Dokumentation zur Swift-Fehlerbehandlung: https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/ErrorHandling.html –

+1

Danke, ich sehe das Beispiel init (contentsOfURL url: NSURL). Ich bin mir immer noch nicht sicher, wie ich es ändern soll. Ich habe noch nie "werfen" gesehen. – Lou

+0

SO ist kein guter Ort für Tutorials. Ich schlage vor, dass Sie im Internet nach einem Tutorial zur Swift-Fehlerbehandlung suchen. Beispiel: https://www.hackingwithswift.com/new-syntax-swift-2-error-handling-try-catch –

Antwort

1

Der Grund, warum Sie fatal error: unexpectedly found nil while unwrapping an Optional value wegen der ! ist immer sind in dieser Codezeile:

let audioPath = NSBundle.mainBundle().pathForResource("Pugs.m4a", ofType: nil)! 

Es stürzt ab, weil Sie ! zu forcieren u nwrap der von pathForResource(_:ofType:) zurückgegebene Wert, der unsicher ist. Wenn der Wert nil ist, erhalten Sie den Fehler unexpectedly found nil. Sie sollten wirklich nur Dinge auspacken, wenn Sie wissen, dass sie nicht nil sein werden.


Probieren Sie etwas wie diese stattdessen tun:

Option 1:

guard let audioPath = NSBundle.mainBundle().pathForResource("Pugs.m4a", ofType: nil) else { 
    // The resource does not exist, so the path is nil. 
    // Deal with the problem in here and then exit the method. 
} 

// The resource exists, so you can use the path. 

Option 2:

Verwendung optional Bindung, wie folgt aus:

if let audioPath = NSBundle.mainBundle().pathForResource("Pugs.m4a", ofType: nil) { 

    // The resource exists, and now you have the path, so you can use it. 

    let url = NSURL(fileURLWithPath: audioPath) 

    do { 

     if playPug == 1 { 
      let sound = try AVAudioPlayer(contentsOfURL: url) 
      player = sound 
      sound.play() 
      playPug = 2 
      playButton.setImage(UIImage(named:"pause_Icon.png"),forState:UIControlState.Normal) 
     } else { 
      player.pause() 
      playPug = 1 
      playButton.setImage(UIImage(named:"play_Icon.png"),forState:UIControlState.Normal) 
     } 

    } catch { 
     print(error) 
    } 

} else { 
    // The path was nil, so deal with the problem here. 
} 
Verwandte Themen