Call a URL from a JSON image to UITableView

0

In the data of my wordpress json I have the following:

 [
{
id: 10033,
title: {
rendered: "My title"
},
content: {
rendered: "Sample text html",
}
_embedded: {
wp:featuredmedia: [
{
source_url: "https://domain.com/myimage.jpg",
}
]
}
}

I have my structures in the OffersData.swift file

struct JsonSosTalleres : Codable {
    let id : Int
    let title : Title?
    let content : Content?
    let embedded : Embedded?

    enum CodingKeys: String, CodingKey {
        case id = "id"
        case title = "title"
        case content = "content"
        case embedded = "_embedded"
    }

}


struct Title : Codable {
    let rendered : String?
    enum CodingKeys: String, CodingKey {
        case rendered = "rendered"
    }
}

struct Content : Codable {
    let rendered : String?
    enum CodingKeys: String, CodingKey {
        case rendered = "rendered"
    }
}


struct Embedded : Codable {
    let featuredmedia : [Featuredmedia]?    
    enum CodingKeys: String, CodingKey {
        case featuredmedia = "wp:featuredmedia"
    }
}

struct Featuredmedia : Codable {
    let urlimage : String
    enum CodingKeys: String, CodingKey {
        case urlimage = "source_url"
    }
}

All this data I want to pass to a UITableView using JSON Decodable, I get the title correctly in the table but I have problems to call the url of the image:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {



        guard let cell = tablaOfertas.dequeueReusableCell(withIdentifier: "ofertacell") as? OfertasTableViewCell else { return UITableViewCell() }

        cell.tituloPost.text = datosOfertas[indexPath.row].title?.rendered // funciona ok

        cell.imagePost.text = datosOfertas[indexPath.row].embedded?.featuredmedia?.urlimage // error :  Value of type '[Featuredmedia]' has no member 'urlimage'

        return cell

    }

Any suggestions why it does not work or how would you call the URL of the image?

The title if it catches me correctly, but with the code this code for a iamgen:

cell.imagePost.text = datosOfertas[indexPath.row].embedded?.featuredmedia?.urlimage

me dice: Value of type '[Featuredmedia]' has no member 'urlimage'
    
asked by Mario Burga 23.03.2018 в 22:23
source

2 answers

-2

Solved:

cell.imagePost.text = datosOfertas[indexPath.row].embedded?.featuredmedia?[0].urlimage
    
answered by 26.03.2018 / 06:14
source
0
_embedded: {
wp:featuredmedia: **[**
{
 source_url: 
"https://domain.com/
myimage.jpg",
}
**]**
 }
}

The brackets that you have in your JSON indicate that it is an array and your first element has the value you are looking for in swift 4, it would be like this ...

cell.imagePost.text = datosOfertas[indexPath.row].embedded?.featuredmedia?.first.urlimage

Although not if you want to load the image in a UIImageView you must use that url to download the image ...

let urlString = datosOfertas[indexPath.row].embedded?.featuredmedia?.first.urlimage
let url = URL(string: urlString)
let data = try? Data(contentsOf: url!) // asegurate que la imagen existe en este url, si no, has unwrap y usa try/catch

imageView.image = UIImage(data: data!)
    
answered by 14.04.2018 в 00:56