Swift 调用中的参数标签不正确?
请帮帮我!!!我最近才开始用Swift编程。这是我的第一个项目。我收到错误消息:“调用中的参数标签不正确”。这是我的代码:Swift 调用中的参数标签不正确?,swift,error-handling,swift2,Swift,Error Handling,Swift2,请帮帮我!!!我最近才开始用Swift编程。这是我的第一个项目。我收到错误消息:“调用中的参数标签不正确”。这是我的代码: import UIKit import AVFoundation class PlaySoundsViewController: UIViewController { var audioPlayer:AVAudioPlayer! override func viewDidLoad() { super.viewDidLoad() // Do any ad
import UIKit
import AVFoundation
class PlaySoundsViewController: UIViewController {
var audioPlayer:AVAudioPlayer!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
if var filePath = NSBundle.mainBundle().pathForResource("psl",ofType: "mp3"){
var filePathUrl = NSURL.fileURLWithPath(filePath)
audioPlayer = AVAudioPlayer(contentsOfURL: filePathUrl, error: nil)
}else {
print("the filepath is empty")
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func playSoundSlow(sender: UIButton) {
audioPlayer.play()
我不确定哪里出了问题,这里是我的代码的另一张图片,因此您也可以看到错误消息
我想让它播放一个叫做psl.mp3的mp3。
请帮帮我!!!我刚开始,不知道该怎么办
顺便说一句,我不是一个英语母语人士,很抱歉出错。Swift 2添加了新的错误处理,这意味着您甚至不必传入错误参数:
audioPlayer = try! AVAudioPlayer(contentsOfURL: filePathUrl)
此初始值设定项抛出
,这意味着如果有错误,您可以在do catch
语句中捕获它。但是,由于您传递了error参数的nil
,因此我假设您确定播放器在那里。这就是为什么我使用试试看
不带do catch
而不是在do catch
中尝试
阅读有关新错误处理的信息。试试这个
do {
audioPlayer = try AVAudioPlayer(contentsOfURL: NSURL.fileURLWithPath(path))
audioPlayer.delegate = self
audioPlayer.prepareToPlay()
audioPlayer.play()
} catch {
print("Catch error in playUsingAudioPlayer")
}
@梅里瑟夫:既然成功了,你能接受我的回答吗