Expected Declarations, Consecutive Declarations on a line must be separated by ';' Cause

Asked 2 years ago, Updated 2 years ago, 43 views

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!")
}
}

swift

2022-09-30 21:11

1 Answers

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.


2022-09-30 21:11

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.