1

I'm new to swift development, I followed a tutorial and everything was fine until I came across this error and I don't know how to fix it. Can you help me ?

if let json = response.result.value {
    let jsonArray:NSArray = json as! NSArray
    /// On fait la boucle pour avoir la liste globale des éléments a afficher
    for i in 0..<jsonArray.count {
        self.OPlays.append(Playlist(
            title:(jsonArray[i] as AnyObject).value(forKey: "title") as? String,
                artist:(jsonArray[i] as AnyObject).value(forKey: "artist") as? String,
                categorie:(jsonArray[i] as AnyObject).value(forKey: "categorie") as? String,
                cover_url:(jsonArray[i] as AnyObject).value(forKey: "cover_url") as? String)
        )
    }
    self.tableViewPlaylist.reloadData()
}        

News file correct.

if let json = response.result.value {
    let jsonArray:NSArray = json as! NSArray

    /// On fait la boucle pour avoir la liste globale des éléments a afficher
    for i in 0..<jsonArray.count {
        self.OPlays.append(Playlist(
            id: (jsonArray[i] as AnyObject).value(forKey: "id") as? Int,
            title: (jsonArray[i] as AnyObject).value(forKey: "title") as? String,
            artist: (jsonArray[i] as AnyObject).value(forKey: "artist") as? String,
            cover_url: (jsonArray[i] as AnyObject).value(forKey: "cover_url") as? String,
            categorie: (jsonArray[i] as AnyObject).value(forKey: "categorie") as? String
        ))
    }
    self.tableViewPlaylist.reloadData()
}
vadian
  • 274,689
  • 30
  • 353
  • 361
Obosso Tv
  • 13
  • 4
  • 1
    Finally, I just found a solution. I forgot to enter the id. – Obosso Tv Jan 16 '20 at 15:54
  • 2
    Look for a real Swift tutorial, the code is horribly *objective-c-ish*. – vadian Jan 16 '20 at 16:15
  • In the future, if you’re posting a question about a tutorial (or any online source), please include link to it. – Rob Jan 16 '20 at 16:44
  • 1
    ... but if this code snippet is from this tutorial, I agree with Vadian, and would suggest you find a better tutorial. This code is not at all encouraging. – Rob Jan 16 '20 at 16:52

1 Answers1

3

I’d excise all of that AnyObject code:

if let array = response.result.value as? [[String: Any]] {
    for dictionary in array {
        self.OPlays.append(Playlist(
            id: dictionary["id"] as? Int,
            title: dictionary["title"] as? String,
            artist: dictionary["artist"] as? String,
            categorie: dictionary["categorie"] as? String,
            cover_url: dictionary["cover_url"] as? String
        ))
    }
    self.tableViewPlaylist.reloadData
}

Personally, I’d go a step further and get out of the business of decoding your JSON manually. Use JSONDecoder.

struct Playlist: Codable {
    let id: Int?
    let title: String?
    let artist: String?
    let categorie: String?
    let cover_url: String?
}

Then, assuming you have data that is the unwrapped Data object:

do {
    self.OPlays = JSONDecoder().decode([Playlist].self, from: data)
} catch {
    print(error)
}

Or, if you’re using Alamofire, consider Alamofire 5 which has a JSONDecoder-based response method.

Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • I'm new, I don't even know where to put every portion of the code except the first. I guess it stays in my viewcontroller. Finally the first code extracted from the tutorial https://www.youtube.com/watch?v=29FEEvLFoLo works. But it should be optimized soon, I will try your code. Thanks again for the quick response. I love swift, I want to perform. – Obosso Tv Jan 16 '20 at 20:22
  • 1
    For now, while you’re still getting you programming sea-legs, the view controller is the easy place to put it. Longer term, we generally want to keep our view controllers fairly minimal, restricted to just filling in UI controls and responding to user input, and move complicated network code and response parsing into a separate object (a separate API manager/controller object of your own creation). That helps to fight “view controller bloat” and is more in the spirit of the “single responsibility principle”. But I might suggest deferring that until you get it working. – Rob Jan 16 '20 at 20:48
  • Sorry, I really don't know anything, I'm in full discovery and I'm just redoing the tutorials and assemblies to create an application. I don't have MVC logic for development yet. But I know it will happen soon. – Obosso Tv Jan 17 '20 at 20:33