Model:
struct StreamCell: Decodable {
var idStream: Int?
var streamTitle: String?
var numOfPlays: Int64?
var streamArtwork: String?
var streamLocation: String?
var streamArtist: Int?
private enum CodingKeys: String, CodingKey {
case idStream = "id_stream"
case streamTitle = "stream_title"
case numOfPlays = "num_of_plays"
case streamArtwork = "stream_artwork"
case streamLocation = "stream_location"
case streamArtist = "stream_artist"
}
init(from decoder: Decoder) throws { (without this I get an error
}
Decodable:
let streams = try JSONDecoder().decode([StreamCell].self, from: data)
print("Titulo: ", streams)
let str = String(data: data, encoding: .utf8)
print(str)
Output:
Retrived Info: [music_app.StreamCell(idStream: nil, streamTitle: nil, numOfPlays: nil, streamArtwork: nil, streamLocation: nil, streamArtist: nil), music_app.StreamCell(idStream: nil, streamTitle: nil, numOfPlays: nil, streamArtwork: nil, streamLocation: nil, streamArtist: nil)]
Json: Optional("[{\"id_stream\":1,\"stream_title\":\"Padecer\",\"num_of_plays\":455,\"artist_artwork\":\"/Users/maurobenedito/Desktop/music_images/artist_picture/artist_picture\",\"stream_artist\":\"1\",\"stream_location\":\"dcfghvbjknlm,\",\"stream_artwork\":\"/Users/maurobenedito/Desktop/music_images/artwork/img_music1\"},{\"id_stream\":2,\"stream_title\":\"Fogo Fogo Incendio\",\"num_of_plays\":45,\"artist_artwork\":\"/Users/maurobenedito/Desktop/music_images/artist_picture/artist_picture\",\"stream_artist\":\"1\",\"stream_location\":\"dcfghvbjknlm,\",\"stream_artwork\":\"/Users/maurobenedito/Desktop/music_images/artwork/img_music1\"}]")
Why am I getting everything nill? No exception error was thrown therefore JSonDecoder worked but my array comes with nothing.
Any help please
Benedito Mauro
5 years ago
Server response:
[
{
"id_stream":1,
"stream_title":"Padecer",
"num_of_plays":455,
"artist_artwork":"/Users/maurobenedito/Desktop/music_images/artist_picture/artist_picture",
"stream_artist":"1",
"stream_location":"dcfghvbjknlm,",
"stream_artwork":"/Users/maurobenedito/Desktop/music_images/artwork/img_music1"
},
{
"id_stream":2,
"stream_title":"Fogo Fogo Incendio",
"num_of_plays":45,
"artist_artwork":"/Users/maurobenedito/Desktop/music_images/artist_picture/artist_picture",
"stream_artist":"1",
"stream_location":"dcfghvbjknlm,",
"stream_artwork":"/Users/maurobenedito/Desktop/music_images/artwork/img_music1"
}
]