Expected '{' to start the body of for-each loop Error

Hey. I'm new to coding. I'm trying to make this simple soundboard, but I keep getting this mistake. I don't know why.



import UIKit

import AVFoundation

class ViewController: UIViewController {


let soundFilenames = ["sound1", "sound2", "sound3", "sound4", "sound5"]

var audioPlayers = [AVAudioPlayer]()

override func viewDidLoad() {

super.viewDidLoad()

/

/

for sound in soundFilenames

do { ---> HERE, I KEEP GETTING A " EXPECTED '{' TO START THE BODY OF FOR-EACH LOOP "

let audioPath = Bundle.main.path(forResource: sound, ofType: "wav"),

try audioPlayers = AVAudioPlayer(contentsOf: NSURL(fileURLWithPath: audioPath!))

}

catch

{

}


}

override func didReceiveMemoryWarning() {

super.didReceiveMemoryWarning()

/

}

}

Accepted Answer

There's no "do" in the Swift for statement.


So your code

for sound in soundFilenames


do { ---> HERE, I KEEP GETTING A " EXPECTED '{' TO START THE BODY OF FOR-EACH LOOP "

let audioPath = Bundle.main.path(forResource: sound, ofType: "wav"),

try audioPlayers = AVAudioPlayer(contentsOf: NSURL(fileURLWithPath: audioPath!))

}

should just be


for sound in soundFilenames

{

let audioPath = Bundle.main.path(forResource: sound, ofType: "wav"),

try audioPlayers = AVAudioPlayer(contentsOf: NSURL(fileURLWithPath: audioPath!))

}

without the "do".

I deleted the "do" and it told me "call can throw, but it is not marked with t"try" and the error is not handled

What version of Swift are you using? If Swift 3, then you should be using URL, not NSURL.


Make sure there are no other errors on the same line as the "call can throw …" error. It might be masking the primary error message.


Also, you will need a do…catch construct somewhere, to catch errors from the "try"ed line, since your viewDidLoad override can't throw to its caller.


If you're using Swift 2, I'm not sure that it works to put the "try" on the LHS of an assignment. You could try it after the "=". (But it seems to be fine in Swift 3, which is all I have to test with right now.)


Also, I see a comma at the end of the "let audioPath = …". That's going to mess you up, and it should likely be removed.


Also, side issue, don't use the path versions of the Bundle class's methods. There are URL equivalents that are preferable. These days, URLs are always preferable to paths, except in exceptional cases.

My apologies, I was looking at what the compiler was complaining about concerning the for loop, and not what you were trying to do with the do ... catch statement; and then guessing that you were coming from a similar-but-different programming language.


I'm waiting for Xcode to reinstall on my laptop, but as much as they make a big deal about removing unnecessary punctuation in Swift, all of the For-In examples in the programming language guide have the for loop specified as

for var in expression {
// body
}

with { and }, so I'd try

for sound in soundFilenames {
  do {
                let audioPath = Bundle.main.path(forResource: sound, ofType: "wav"),
     try audioPlayers = AVAudioPlayer(contentsOf: NSURL(fileURLWithPath: audioPath!))
         
        }

        catch
        {
     
        }
}
}

That would address the syntax error you're being given for the For-In loop. I'm not sure whether it's allowed to put the try togther with the let statement. You may have to declare audioPlayers in a seperate let statement.

Expected '{' to start the body of for-each loop Error
 
 
Q