In my app I currently use a separate class
to store a bunch of static variables that I want to access from anywhere at anytime.
class Sounds {
static var soundInitial : Sound!
static var soundThunder : Sound!
static var soundWind : Sound!
// etc... (about 50 more)
}
This Sounds
class is never being used as an instance, but figures as a container for all the sound-effects (which are of type Sound
- which is a separate class)
I initialize all the sound-effects from the Main View Controller during viewDidLoad
:
let urlPath1 = Bundle.main.url(forResource: "art.scnassets/sounds/my_sound_file", withExtension: "mp3")
Sounds.soundInitial = Sound(url: urlPath1!, volume: 1.0)
// etc...
Then I can use it at any given time like so:
Sounds.soundInitial.play(numberOfLoops: -1) // endless looping
My very basic question as a not-yet-so-well-experienced-programmer:
Is there a more common approach or better way to store all my sound variables centrally together? (I don't want to define them in View Controller, because this would give me too much variables there...)
I found out, that I could use a struct
to achieve the same. But would this be a better approach?
Is there a special Container Object in Swift designed for such purposes?