Posts: 1
Joined: Fri Nov 23, 2018 10:13 am
Country: New Zealand

api use for swift

Fri Nov 23, 2018 10:18 am

Hi there

I am making an ios app using by your api
now I am doing like below code
but I got the error say,
""signal SIGABRT"
in this part let d = json["album"] as! [String: Any]

so could you let me know how can I write?
Thank you

let searchURL = URL(string: "https://theaudiodb.com/api/v1/json/195003/searchalbum.php?s=\(text)")

let urlRequest = URLRequest(url: searchURL!)
let task = URLSession.shared.dataTask(with: urlRequest) {
(data, response, error)
if(error == nil) {
do {
let json = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as! [String: Any]
let d = json["album"] as! [String: Any]

//let random = (Int)(arc4random_uniform((UInt32)(data.count)))
//let random = Int.random(in: 0..<data.count)
let image_url = d["strAlbumThumb"] as! String

// showing image to ImageView
let url = URL(string: image_url)

DispatchQueue.main.async {
let image_data = try! Data(contentsOf: url!)
// save data
self.pictureView.image = UIImage(data: image_data)
self.titleLabel.text = d["strAlbum"] as? String
self.artistLabel.text = d["strArtist"] as? String
self.genreLabel.text = d["strGenre"] as? String

User avatar
Site Admin
Site Admin
Posts: 1252
Joined: Wed Jun 06, 2012 9:19 am
Country: United Kingdom

Re: api use for swift

Fri Nov 23, 2018 11:53 am

sorry can't help with Swift, but our structure is valid JSON so it should work just like any other array

Return to “Developers”