'return' 之后的代码永远不会被执行
Code after 'return' will never be executed
我正处于编码的第一周。
到目前为止,我在 swift 中制作的 youtube 教程应用程序与上述错误消息完全不同。这是代码。我现在已经完全按照他们指定的方式重写了两次。
我正在尝试设置声音来玩我们正在制作的纸牌游戏,当纸牌被洗牌、翻转、匹配或不正确匹配时播放声音。
"let soundURL" 代码行 "Code after 'return' will never be executed" 显示错误消息。
请帮忙?
class SoundManager {
static var audioPlayer:AVAudioPlayer?
enum SoundEffect {
case flip
case shuffle
case match
case nomatch
}
static func playSound(_ effect:SoundEffect) {
var soundFilename = ""
// Determine which sound effect we want to play
//and set the appropriate file name
switch effect {
case .flip:
soundFilename = "cardflip"
case .shuffle:
soundFilename = "cardflip"
case .match:
soundFilename = "dingcorrect"
case.nomatch:
soundFilename = "dingwrong"
}
// Get the path to the sound file inside the bundle
let bundlePath = Bundle.main.path(forResource: soundFilename, ofType: "wav")
guard bundlePath != nil else {
print("Couldn't find sound file \(soundFilename) in the bundle")
return
// Create a URL object from this string path
let soundURL = URL(fileURLWithPath: bundlePath!)
do {
// Create audio player object
audioPlayer = try AVAudioPlayer(contentsOf: soundURL)
// Play the sound
audioPlayer?.play()
}
catch {
// Could'nt create audio player object, log the error
print("Could'nt create the audio player object for sound file \(soundFilename)")
}
}
}
}
我相信您希望您的代码看起来像这样:
// Get the path to the sound file inside the bundle
let bundlePath = Bundle.main.path(forResource: soundFilename, ofType: "wav")
guard bundlePath != nil else {
print("Couldn't find sound file \(soundFilename) in the bundle")
return
}
// Create a URL object from this string path
let soundURL = URL(fileURLWithPath: bundlePath!)
do {
// Create audio player object
audioPlayer = try AVAudioPlayer(contentsOf: soundURL)
// Play the sound
audioPlayer?.play()
}
catch {
// Could'nt create audio player object, log the error
print("Could'nt create the audio player object for sound file \(soundFilename)")
}
如果 guard
语句的括号内有 return
,则您不希望在 return
下方和同一括号内放置任何代码。 return
下面的代码 None 将执行。在我的示例中,以下代码在括号 之外 ,这意味着只要 bundlePath
.
有值,它就会执行
Code after 'return' will never be executed
var soundFilename = ""
// Determine which sound effect we want to play
//and set the appropriate file name
switch effect {
case .flip:
soundFilename = "cardflip"
case .shuffle:
soundFilename = "cardflip"
case .match:
soundFilename = "dingcorrect"
case.nomatch:
soundFilename = "dingwrong"
}
// Get the path to the sound file inside the bundle
let bundlePath = Bundle.main.path(forResource: soundFilename, ofType: "wav")
guard bundlePath != nil else {
print("Couldn't find sound file \(soundFilename) in the bundle")
return
}
// Create a URL object from this string path
let soundURL = URL(fileURLWithPath: bundlePath!)
do {
// Create audio player object
audioPlayer = try AVAudioPlayer(contentsOf: soundURL)
// Play the sound
audioPlayer?.play()
}
catch {
// Could'nt create audio player object, log the error
print("Could'nt create the audio player object for sound file \(soundFilename)")
}
// }
}
我相信你误解了guard语法,因为它会在条件正确时执行代码,而在条件不正确时落入else部分。因此它将在 else 中使用 return 而没有其他代码,这样该方法将优雅地终止而不会导致任何崩溃。
return关键字用于return方法执行或退出方法。
所以需要用“}”关闭else部分,去掉下面的catch块。希望这有帮助。
我正处于编码的第一周。 到目前为止,我在 swift 中制作的 youtube 教程应用程序与上述错误消息完全不同。这是代码。我现在已经完全按照他们指定的方式重写了两次。 我正在尝试设置声音来玩我们正在制作的纸牌游戏,当纸牌被洗牌、翻转、匹配或不正确匹配时播放声音。 "let soundURL" 代码行 "Code after 'return' will never be executed" 显示错误消息。 请帮忙?
class SoundManager {
static var audioPlayer:AVAudioPlayer?
enum SoundEffect {
case flip
case shuffle
case match
case nomatch
}
static func playSound(_ effect:SoundEffect) {
var soundFilename = ""
// Determine which sound effect we want to play
//and set the appropriate file name
switch effect {
case .flip:
soundFilename = "cardflip"
case .shuffle:
soundFilename = "cardflip"
case .match:
soundFilename = "dingcorrect"
case.nomatch:
soundFilename = "dingwrong"
}
// Get the path to the sound file inside the bundle
let bundlePath = Bundle.main.path(forResource: soundFilename, ofType: "wav")
guard bundlePath != nil else {
print("Couldn't find sound file \(soundFilename) in the bundle")
return
// Create a URL object from this string path
let soundURL = URL(fileURLWithPath: bundlePath!)
do {
// Create audio player object
audioPlayer = try AVAudioPlayer(contentsOf: soundURL)
// Play the sound
audioPlayer?.play()
}
catch {
// Could'nt create audio player object, log the error
print("Could'nt create the audio player object for sound file \(soundFilename)")
}
}
}
}
我相信您希望您的代码看起来像这样:
// Get the path to the sound file inside the bundle
let bundlePath = Bundle.main.path(forResource: soundFilename, ofType: "wav")
guard bundlePath != nil else {
print("Couldn't find sound file \(soundFilename) in the bundle")
return
}
// Create a URL object from this string path
let soundURL = URL(fileURLWithPath: bundlePath!)
do {
// Create audio player object
audioPlayer = try AVAudioPlayer(contentsOf: soundURL)
// Play the sound
audioPlayer?.play()
}
catch {
// Could'nt create audio player object, log the error
print("Could'nt create the audio player object for sound file \(soundFilename)")
}
如果 guard
语句的括号内有 return
,则您不希望在 return
下方和同一括号内放置任何代码。 return
下面的代码 None 将执行。在我的示例中,以下代码在括号 之外 ,这意味着只要 bundlePath
.
Code after 'return' will never be executed
var soundFilename = ""
// Determine which sound effect we want to play
//and set the appropriate file name
switch effect {
case .flip:
soundFilename = "cardflip"
case .shuffle:
soundFilename = "cardflip"
case .match:
soundFilename = "dingcorrect"
case.nomatch:
soundFilename = "dingwrong"
}
// Get the path to the sound file inside the bundle
let bundlePath = Bundle.main.path(forResource: soundFilename, ofType: "wav")
guard bundlePath != nil else {
print("Couldn't find sound file \(soundFilename) in the bundle")
return
}
// Create a URL object from this string path
let soundURL = URL(fileURLWithPath: bundlePath!)
do {
// Create audio player object
audioPlayer = try AVAudioPlayer(contentsOf: soundURL)
// Play the sound
audioPlayer?.play()
}
catch {
// Could'nt create audio player object, log the error
print("Could'nt create the audio player object for sound file \(soundFilename)")
}
// }
}
我相信你误解了guard语法,因为它会在条件正确时执行代码,而在条件不正确时落入else部分。因此它将在 else 中使用 return 而没有其他代码,这样该方法将优雅地终止而不会导致任何崩溃。
return关键字用于return方法执行或退出方法。
所以需要用“}”关闭else部分,去掉下面的catch块。希望这有帮助。