将图像URL从JSON调用到tableview文件

时间:2018-03-24 00:36:26

标签: ios swift uitableview

在我的WordPress json的数据中,我有以下内容:

//get the working directory
    MagicStrings.root_path = Environment.getExternalStorageDirectory().toString() + "/seedlet";
    System.out.println("Working Directory = " + MagicStrings.root_path);
    AIMLProcessor.extension = new PCAIMLProcessorExtension();
    //Assign the AIML files to bot for processing
    bot = new Bot("seedlet", MagicStrings.root_path, "chat");
    chat = new Chat(bot);
    String[] args = null;
    mainFunction(args);

}

我在OfertasTableViewCell.swift文件中有我的结构

 [
{
id: 11,
title: {
rendered: "My title post"
},
content: {
rendered: "Content text html",
}
_embedded: {
wp:featuredmedia: [
{
source_url: ".../myimage.jpg",
}
]
}
}

因为标题已经有效,但我有问题需要获取图片的网址:

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"
    }
}

为什么它不起作用的任何建议或如何调用图像的URL?

使用以下代码:

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



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

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

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

        return cell

    }

收到以下错误:类型的值' [Featuredmedia]'没有会员' urlimage'

1 个答案:

答案 0 :(得分:0)

featuredmedia是一个数组而不是单个对象,你可以尝试

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