致命错误:在打开时意外发现零

时间:2015-09-03 20:14:28

标签: swift

为什么我得到主题1:EXE_BAD_INSTRUCTION

这是我的代码

    override func viewDidAppear(animated: Bool) {

    var fileclocaiton = NSString(string: NSBundle.mainBundle().pathForResource(self.navigationItem.title, ofType: "mp3")!)
    var error: NSError? = nil
    player = AVAudioPlayer(contentsOfURL: NSURL(string: fileclocaiton as String), error: &error)
}

1 个答案:

答案 0 :(得分:0)

var fileclocaiton = NSString(string: NSBundle.mainBundle().pathForResource(self.navigationItem.title, ofType: "mp3")!)

此行是崩溃的来源。您确定您拥有与navigationItem.title相同的资源吗? navigationItem.title与UIViewController上的title属性相同吗?您必须尝试并确保存在该类型的资源。如果使用if as,

找不到资源,则可以避免崩溃
if let fileclocaiton = NSString(string: NSBundle.mainBundle().pathForResource(self.navigationItem.title, ofType: "mp3")) {

     ... do rest of your coding here.
}