gpt4 book ai didi

swift - 无法转换类型和参数标签的值...重载

转载 作者:行者123 更新时间:2023-11-28 06:52:16 25 4
gpt4 key购买 nike

我尝试将资源文件复制到我的 swift 编码中,但出现了这些错误。xCode,Swift:无法转换“NSError”类型的值?到预期的参数类型“字符串?”和 参数标签“(contentsOfURL:, error:)”不匹配任何可用的重载

这是我的代码

public class SKTAudio {
public var backgroundMusicPlayer: AVAudioPlayer?
public var soundEffectPlayer: AVAudioPlayer?

public class func sharedInstance() -> SKTAudio {
return SKTAudioInstance
}

public func playBackgroundMusic(filename: String) {
let url = NSBundle.mainBundle().URLForResource(filename, withExtension: nil)
if (url == nil) {
print("Could not find file: \(filename)")
return
}

var error: NSError? = nil
backgroundMusicPlayer = AVAudioPlayer(contentsOfURL: url!, fileTypeHint: error)`Cannot convert value of type 'NSError?' to expected argument type 'String?'`
if let player = backgroundMusicPlayer {
player.numberOfLoops = -1
player.prepareToPlay()
player.play()
} else {
print("Could not create audio player: \(error!)")
}
}

public func pauseBackgroundMusic() {
if let player = backgroundMusicPlayer {
if player.playing {
player.pause()
}
}
}

public func resumeBackgroundMusic() {
if let player = backgroundMusicPlayer {
if !player.playing {
player.play()
}
}
}

public func playSoundEffect(filename: String) {
let url = NSBundle.mainBundle().URLForResource(filename, withExtension: nil)
if (url == nil) {
print("Could not find file: \(filename)")
return
}

var error: NSError? = nil
soundEffectPlayer = AVAudioPlayer(contentsOfURL: url, error: &error)`Argument labels '(contentsOfURL:, error:)' do not match any available overloads`
if let player = soundEffectPlayer {
player.numberOfLoops = 0
player.prepareToPlay()
player.play()
} else {
print("Could not create audio player: \(error!)")
}
}
}

private let SKTAudioInstance = SKTAudio()

这是我正在尝试做的事情: http://www.raywenderlich.com/100496/video-tutorial-how-to-make-a-game-like-flappy-bird-in-swift-part-1-scene-size-and-aspect-ratios

最佳答案

我检查了代码,发现行中有错误

backgroundMusicPlayer = AVAudioPlayer(contentsOfURL: url!, fileTypeHint: error)

这里您将错误实例作为参数传递,而函数需要一个字符串。

要解决此问题,您可以传递一个表示文件格式的字符串。例如:AVFileTypeMPEG4AVFileTypeAppleM4VAVFileTypeCoreAudioFormat

 backgroundMusicPlayer = AVAudioPlayer(contentsOfURL: url!, fileTypeHint: AVFileTypeMPEG4)

关于swift - 无法转换类型和参数标签的值...重载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34566286/

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