繁体   English   中英

xcode9 swift4致命错误:展开一个可选值时意外发现nil

[英]xcode9 swift4 Fatal error: Unexpectedly found nil while unwrapping an Optional value

应用启动时,我试图播放mp3文件(5秒),但不断出现错误:

线程1:致命错误:展开一个可选值时意外地找到零

import UIKit
import AudioToolbox

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    //create SystemSoundID
    var soundID:SystemSoundID = 0
    let path = Bundle.main.path(forResource: "iPhone100Voice", ofType: "mp3")
    let baseURL = NSURL(fileURLWithPath: path!)
    AudioServicesCreateSystemSoundID(baseURL, &soundID)
    //play
    AudioServicesPlaySystemSound(soundID)

    }

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
    }

}

谁能解释为什么以及如何解决这个问题?

检查以下几点,所有条件都必须为真:

  • 该文件是否在项目导航器中?
  • 在文件检查器中是否选中了“ Target Membership复选框(要获取它,请选择文件并按⌥⌘1
  • 文件名的拼写是否正确( iPhone100Voice.mp3 )?

请检查您的文件名,也许文件名不正确或捆绑包中没有

因此,请检查带有扩展名的文件名。

if letguard unwrap路径,请使用:

override func viewDidLoad() {

    super.viewDidLoad()
    var soundID:SystemSoundID = 0

    if let path = Bundle.main.path(forResource: "iPhone100Voice", ofType: "mp3") {
       let baseURL = NSURL(fileURLWithPath: path)
       AudioServicesCreateSystemSoundID(baseURL, &soundID)
       AudioServicesPlaySystemSound(soundID)
    } else {
        let alert = UIAlertController(title: "Alert", message: "File not found in your bundle", preferredStyle: UIAlertControllerStyle.alert)
        alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.cancel, handler: nil))
        self.present(alert, animated: true, completion: nil)

   }
}

播放视频可能对您有所帮助。 将视频文件添加到项目中时,请确保在文件检查器中选择了“目标”复选框。

override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(true)


        if  let path = Bundle.main.path(forResource: "video_1523966087401 (1)", ofType: ".mp4"){
            let player = AVPlayer(url: URL(fileURLWithPath: path))
            let playerController = AVPlayerViewController()
            playerController.player = player
            present(playerController, animated: true) {
                player.play()
            }
        }else{
            //show dialog to user

            print("Error: File not found")
        }
    }

! 表示崩溃运算符,它解开了可选值。 比如说,那里的价值就像Optional(53)一样。 因此,当path!时,变量应包含nil以外的值path! 尝试解开可选内容并在路径变量中附加值53。

为了解决这个问题,我们可以使用两种方法

使用警卫

guard let pathValue = path else {
        return
}

let baseURL = NSURL(fileURLWithPath: pathValue) //no need to use pathValue, if you use then you have to use path!, although it makes no crash now.  
AudioServicesCreateSystemSoundID(baseURL, &soundID)
//play
AudioServicesPlaySystemSound(soundID)

如果path为nil,则进入该块并返回;如果它具有value,则继续到下一行。

使用if-let,了解optional binding

if let pathValue = path {
    let baseURL = NSURL(fileURLWithPath: pathValue) //same comment
    AudioServicesCreateSystemSoundID(baseURL, &soundID)
    //play
    AudioServicesPlaySystemSound(soundID)
} 

现在,如果path是可选的,则无法到达块并传递到下一行

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM