2016-07-29 10 views
-3

私はこれにかなり新しいので、タイトルのエラーを解決するための正しいフォーマットを見つけようとしています。私は次のような行になります:audioPath = NSBundle.mainBundle().pathForResource( "Pugs.m4a"、ofType:nil)!エラー:致命的なエラー:オプションの値をアンラッピングしている間に予期せずnilが見つかりました

私は何かが分からないことがわかっています。

輸入のUIKit 輸入AVFoundation

クラスの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

Appleのマニュアルを見ると、メソッドが 'init(contentsOfURL url:NSURL)'で、Swift 2のエラー処理が使用されていることがわかります。ドキュメンテーションは常にあなたの最初の場所であるべきです。 「AVAudioPlayer」ドキュメントへのリンク:https://developer.apple.com/library/ios/documentation/AVFoundation/Reference/AVAudioPlayerClassReference/#//apple_ref/occ/instm/AVAudioPlayer/initWithContentsOfURL:error:。 Swiftエラー処理ドキュメントへのリンク:https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/ErrorHandling.html –

+1

ありがとう、init(contentsOfURL url:NSURL)メソッドの例があります。私はまだそれを変更する方法がわからない。私は前に "スロー"を見たことがありません。 – Lou

+0

SOはチュートリアルのための良い場所ではありません。 Swiftエラー処理に関するチュートリアルをインターネットで検索することをお勧めします。例:https://www.hackingwithswift.com/new-syntax-swift-2-error-handling-try-catch –

答えて

1

あなたがfatal error: unexpectedly found nil while unwrapping an Optional valueを取得している理由があるため、このコード行で!は次のとおりです。

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

あなたが!にあなたの力を使っているので、クラッシュしていますnwrappathForResource(_:ofType:)によって返される値は安全ではありません。値がnilの場合、unexpectedly found nilエラーが発生します。 nilではないことが分かっているときには、実際にはアンラップだけを強制する必要があります。

オプション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. 

オプション2:オプションの結合

使用、このような:

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. 
} 


ではなく、このような何かをやってみ

関連する問題