I have a follow up question that I bet you know the answer to. Of course no worries if you don't have the time. How would I get [genreNames] into the table? The genreNames that come back are formatted as an Array of Strings. In my data model I had entered the genreNames as [String] as shown here:
struct SongDetails {
var albumName: String
var artistName: String
var artworkURL: String
var composerName: String
var discNumber: Int
var durationInMillis: Int
var genreNames: [String]
var isrc: String
var name: String
var id: String
var releaseDate: String
var trackNumber: Int
but when parsing the JSON when it returns from the API call, it would not let me label the genreNames with .string!
URLSession.shared.dataTask(with: musicRequest) { (data, response, error) in
guard error == nil else { return }
if let json = try? JSON(data: data!) {
let result = (json["data"]).array!
for songDetail in result {
let attributes = songDetail["attributes"]
let song = SongDetails(albumName: attributes["albumName"].string!,
artistName: attributes["artistName"].string!,
artworkURL: attributes["artwork"]["url"].string!,
composerName: attributes["composerName"].string ?? "-",
discNumber: attributes["discNumber"].int!,
durationInMillis: attributes["durationInMillis"].int!,
genreNames: attributes["genreNames"].arrayValue,
isrc: attributes["isrc"].string!,
name: attributes["name"].string!,
id: attributes["playParams"]["id"].string!,
releaseDate: attributes["releaseDate"].string!,
trackNumber: attributes["trackNumber"].int!)
songDetails.append(song)
print(songDetails)
}
so I eventually figured out that it had to be an arrayValue or an Array. So now I have that working but I can't seem to convert the array into a string using join() . I just want a string of the genreNames to put into once table cell.
I also re-wrote everything to use Codable, but got stuck at the east same point. So basically I want to get [genreNames] returned from the JSON to a string such as: Alternative, Indie, Pop. I hope the is enough info for you or else anyone trying to help. This is the last piece I am struggling with.
Thanks!