I got two different Objects Song
and Album
. I got an AlbumView
with a @State var album: Album
and I want to reuse that View but pass a Song
instead. Is it possible to pass either Album
or Song
? Otherwise it would also be helpful if I could set Album
to nil and then just check in the View whether it has a value.
This is what Album
looks like:
struct Album: Identifiable {
let id = UUID()
let name: String
let artist: String
let songs: [AlbumSong]
let releaseDate: Date
let price: Int
let albumImageUrl: String
var unlocked: Bool
}
This is what Song
looks like:
struct Song: Identifiable, Codable {
let id = UUID()
let title: String
let duration: TimeInterval
var image: String
let artist: String
let track: String
let price: Int
}
This is my AlbumView
:
struct AlbumView: View {
@State var album: Album
var body: some View {
Text("\(album.name)").font(.system(size: 18))
}
}
This would be my idea to solve it with passing one object as nil:
struct AlbumView: View {
@State var album: Album?
@State var song: Song
var body: some View {
if album != nil {
Text("\(album!.name)")
} else {
Text("\(song.name)")
}
}
}