When I try to create the below songs
array, I get the error:
"Cannot use instance member 'song' within property initializer; property initializers run before 'self' is available"
class ViewController: UIViewController {
struct Song {
let title: String
}
let song = Song(title: "A")
let songs = [song]
}
I can move let songs = [song]
into viewDidLoad()
, but then I can't access songs
from other functions. I can change let
to var
and then change songs
in viewDidLoad()
to my array of songs, but then I've created a mutable array when I want it to be immutable.
How do I get an immutable array of songs available to all functions and still have each individual song available in its own constant as well?