2016-07-29 3 views
-3

저는 이것에 상당히 익숙하며 제목에서 오류를 해결하기위한 올바른 형식을 찾으려고합니다. 내가 줄을 : audioPath = NSBundle.mainBundle(). pathForResource ("Pugs.m4a", ofType : 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 Error Handling 문서에 링크 : https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/ErrorHandling.html –

+1

감사합니다. init (contentsOfURL url : NSURL) 메소드 예제가 있습니다. 나는 아직도 그것을 바꾸는 방법을 확신하지 못한다. 내가 전에 "던져"본 적이있다 .. – Lou

+0

그래서 자습서를위한 좋은 장소가 아닙니다. 신속한 오류 처리에 대한 자습서를 인터넷으로 검색하는 것이 좋습니다. 예 : 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)! 

당신이 ! to 을 사용하고 있기 때문에 충돌합니다. 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. 
} 
이 같은

사용 옵션 바인딩

관련 문제