0

I'm dealing with an optional that I don't really want to have to deal with, but think do, catch might be able to handle.

do { run(playSound["correct"]!) } catch {print("Failed to find")}

for those not familiar with SpriteKit, playsound("correct") is an action which plays a sound:

I create a lot of them, with a function, and put them in a dictionary where a string key is used to find the right one.

func makePlayable(_ soundFileName: String) -> SKAction {
        let playSoundAction = SKAction.playSoundFileNamed(soundFileName, waitForCompletion: false)
        return playSoundAction
    }

... imagine a for loop here punting a hundred or so of these into a dictionary with a string taken from the file name of the audio to act as key for the action that plays the appropriate sound.

The problem I'm wanting to deal with is that the use of a dictionary to return and play the actions means an optional is coming back.

Which needs unwrapping:

run(playSound["correct"]!)

Perhaps my fears are unfounded, but this instantly made me think I have to somehow do this more gracefully so I can deal with potential failure in the system that's trying to determine the string of the action of the sound file to play... yeah, sorry about that sentence.

The first choice I had was this:

if let soundAction = playSound["correct"]{
            run(soundAction)
            }
        else{ print("Can't find playable sound Action with this name in \(playSound) dictionary")
            }

This struck me as needless and long winded, so I looked at do

But that, when used in the way described at the top of the question, causes a warning indicating the catch is never going to get called.

What am I doing wrong, or am I wrongheaded in thinking do, catch is even appropriate for this?

Confused
  • 6,048
  • 6
  • 34
  • 75
  • You *cannot* catch implicitly unwrapped optionals (or other runtime errors/exceptions), compare http://stackoverflow.com/questions/34628999/swift-force-unwrapping-exception-not-propagated or http://stackoverflow.com/questions/33860288/try-and-catch-around-unwrapping-line-swift-2-0-xcode-7. – Martin R Nov 11 '16 at 14:12
  • `do catch` does not work for force unwraps, they can catch errors, not crashes. I am missing the right terminology here, but `catch` can catch everything caused by a throw, but force unwrapping, index out of bounds etc do not throw an error, they are a bug and rightfully crash. – luk2302 Nov 11 '16 at 14:13
  • argh, thanks @luk2302, I think i get it, errors are things we "create" ourselves. I'm good at creating bugs and crashes, though. Perhaps a specialist. – Confused Nov 11 '16 at 14:22
  • @MartinR i got to the bit about "`ErrorType` protocol " in your answer here: http://stackoverflow.com/questions/34628999/swift-force-unwrapping-exception-not-propagated and the lights came on. "error" is not a common use of the word to describe behaviour in Swift programming, it's a very specific TERM!!! A decree: Language Designers and their minions of documentarians should be forced to create new words rather than repurposing highly used common words. They are, after all, designing a language ;) – Confused Nov 11 '16 at 14:28

0 Answers0