I wanted to create an app to play background music, so I tried to import AVFoundation first and use the do-try-catch syntax, but I was troubled because Expected declarations, Consecutive declarations on a line must be separated by ';
is displayed.
Please tell me the solution.Thank you for your cooperation.
import AVFoundation // Import AVFoundation Framework
classViewController:UIViewController {
varplayer:AVAudioPlayer?//Variables for controlling voice
let url = NSBundle.mainBundle().bundleURL.URLByAppendingPathComponent(soundName); do {
try player = AVAudioPlayer (contentsOfURL:url)
}catch {
print("Error!")
}
}
This is what happens when you modify your code to not use ; and then indent it again.
import AVFoundation // Import AVFoundation Framework
classViewController:UIViewController {
varplayer:AVAudioPlayer?//Variables for controlling voice
let url = NSBundle.mainBundle().bundleURL.URLByAppendingPathComponent(soundName)
do {//<- This do statement is directly below the class definition
try player = AVAudioPlayer (contentsOfURL:url)
} catch{
print("Error!")
}
}
In Swift, execution statements such as do statements cannot be written directly below the class definition.Also, it seems that the variable(?)soundName has not been declared.Declare the required methods and variables, and write the execution statement in the method.Also, if you try to write more than one sentence per line, you should avoid it as much as possible until you get used to it.
© 2024 OneMinuteCode. All rights reserved.