1

In order to fix this error, this person's answer suggested to change the struct to a class. I did that but I'm still getting the error. How else could I fix this problem?

class FilmaManager {
    
    let urlString = "https://jsonplaceholder.typicode.com"
    
    func fetchAlbums(albums: inout [Album]) {
        AF.request("\(urlString)/albums").responseJSON { (response) in
            switch response.result {
            case .success(let value):
                let json = JSON(value)
                
                //debugPrint(json)
                for album in 0 ..< 100 {
                    let albumId = json[album]["id"].int
                    let albumUserId = json[album]["userId"].int
                    let albumTitle = json[album]["title"].string
                    
                    albums.append(Album(id: albumId ?? 0, userId: albumUserId ?? 0, title: albumTitle ?? ""))
                }
            case .failure(let error):
                print(error)
            }
        }
        print(albums)
    }
}
TylerP
  • 9,600
  • 4
  • 39
  • 43
Nico Cobelo
  • 557
  • 7
  • 18
  • 2
    Remove `inout` and use a callback closure to return the value. `inout` should be used rarely, this method is basically unusable without a callback that tells you when the work has been completed. You are not even handling errors. – Sulthan Jan 19 '21 at 22:51
  • In that case, how could I use the callback function to apply the value of albums to a tableview's numberOfRows or CellForRowAt function? – Nico Cobelo Jan 19 '21 at 22:56
  • 2
    Pass a completion callback that will handle the error (e.g. displaying an alert) and if fetched successfully, appends the fetched album to an array variable inside the table and reloads the table. – Sulthan Jan 19 '21 at 22:58
  • I'll try that. Thanks @Sulthan – Nico Cobelo Jan 19 '21 at 22:59

0 Answers0