gpt4 book ai didi

swift - 调用中的参数标签不正确?

转载 作者:搜寻专家 更新时间:2023-10-31 22:33:07 25 4
gpt4 key购买 nike

请帮帮我!!!我最近才开始使用 Swift 编程。这是我的第一个项目。我收到错误消息:“调用中的参数标签不正确”。这是我的代码:

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()

我不确定这里出了什么问题是我的代码的另一张图片,因此您也可以看到错误消息。 My Code

我正在尝试让它播放名为 psl.mp3 的 mp3。请帮我!!!我刚开始,不知道该怎么做。

附言母语不是英语,如有错误请见谅。

最佳答案

Swift 2 添加了新的错误处理,这意味着您甚至不必传入错误参数:

audioPlayer = try! AVAudioPlayer(contentsOfURL: filePathUrl)

这个初始化器 throws ,这意味着如果有错误,您可以在 do-catch 中捕获它陈述。但是,由于您通过了 nil对于错误参数,我假设您确定播放器在那里。这就是我使用 try! 的原因没有 do-catch而不是 trydo-catch .

了解新的错误处理 here .

关于swift - 调用中的参数标签不正确?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35527721/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com