将文本值分配给JSON数据

时间:2016-02-02 16:24:08

标签: ios json swift

我从这样的网址获取JSON数据:

... 
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("jsonCell") as! CeldaGeo
    var dict = arrRes[indexPath.row]
    cell.cofradia.text = dict["cofradia"] as? String
    cell.calle.text = dict["calle"] as? String
    cell.id.text = dict["idc"] as? String
    cell.tipo.text = dict["tipo"] as? String
    cell.latitud.text = dict["latitud"] as? String
    cell.longitud.text = dict["longitud"] as? String
    cell.tipo.text = dict["tipo"] as? String
    let tipo = dict["tipo"] as? String
    let id = dict["idc"] as? String
    cell.imagen.kf_setImageWithURL(NSURL(string: "http://elpenitente.playcofrade.com/img/escudos/\(tipovalue)/\(id!).jpg")!)

    return cell

之后,我从一个url加载一个图像,该图像根据其他值而变化。 我的问题是,值tipo是一个数字(1,2,3),但在url中它应该是文本。例如,1将为cofradias。如何分配此值以便以后可以使用它们?

1 个答案:

答案 0 :(得分:2)

如果我理解你的问题。您只需要一个单独的字典映射id及其字符串。

let imageNames = ["1":"cofradias","2":"string2","3":"string3"]

然后在您的代码中,您将使用它:

let id = dict["idc"] as? String
let tipo = dict["tipo"] as? String
let imageName = imageNames[tipo!]

cell.imagen.kf_setImageWithURL(NSURL(string: "http://elpenitente.playcofrade.com/img/escudos/cofradias/\(imageName!)/\(id!).jpg")!)